matlab, how do i write a statement that will give me time on xaxis from y=0.3

前端 未结 5 1263
情书的邮戳
情书的邮戳 2020-12-11 22:34
x=[0:.01:10];
y=(x.^2).*(exp(-x));

plot(x,y), grid
y1=max(y);

xlabel(\'TIME\');
ylabel(\'CONCENTRATION IN BLOOD\');
title(\'CONCENTRATIN OF SUSTANCE IN BLOOD vs TI         


        
5条回答
  •  南方客
    南方客 (楼主)
    2020-12-11 22:51

    One key issue here is that there are multiple points on your plot where y = 0.3. If you want to find all of them in a simple way, you can follow these steps:

    • Subtract 0.3 from your vector y, so that the points you want to find become zero crossings.
    • Find the indices in the above vector where there is a sign change.
    • For the y values on either side of the zero crossings, compute the percentage of the difference between them at which the value 0.3 lies. This essentially performs a linear interpolation between the two points on either side of the zero crossing.
    • Use the above percentage to find the corresponding value of x for the zero crossing.

    And here's the code along with a plot to show the points found:

    >> yDesired = 0.3;
    >> index = find(diff(sign(y-yDesired)));
    >> pctOfDiff = (yDesired-y(index))./(y(index+1)-y(index));
    >> xDesired = x(index)+pctOfDiff.*(x(index+1)-x(index))
    
    xDesired =
    
        0.8291    3.9528
    
    >> plot(x,y);
    >> hold on;
    >> plot(xDesired,yDesired,'r*')
    >> xlabel('x');
    >> ylabel('y');
    

    enter image description here

提交回复
热议问题