Help Plotting Polynomial and Annotating With Data

Im trying to plot a polynomial function (Non Linear) against an ideal straight line (Linear).
I have managed this with my limited MatLab skills, however i would like to extrapolate some data from the graph which i know can be done as i have done it in the past but cannot work out how to do it again.
I want to determine the maximum non linearity between the two plots and illustrate this on the plot
Below is the code i have so far:
y_1=@(x)+1+2*x+0.05*x.^2-0.00833*x.^3;
x_1=0:6;
figure % new figure window
plot(x_1,y_1(x_1),'--b*')
hold on
x2=0:6;
y2=1+2*x2
plot(x2,y2,'r')
hold off
title('Graph of Sensor Non Linearity')
xlabel('Input Units (I)') % x-axis label
ylabel('Output') % y-axis label
legend('Non Linearity','Ideal Straight Line (ISL)','Location','southeast')
grid on
grid minor
Any help is appreciated

回答(1 个)

How do you define ‘maximum non linearity’?
It would seem to me that it would be defined by the square and cubic terms:
d = 0.05*x.^2-0.00833*x.^3
so you might simply determine the maximum of that expression. Taking the derivative and solving it:
0 = 0.1*x - 0.02499*x^2
gives me a value of x=4.002. The value of d is then 0.26688.
Is that what you want?

4 个评论

Maximum non linearity would be the maximum deviation between the two plots so the point at which the gap is largest, how large is it?
I have to solve this both graphically and then mathematically to verify what I have determined from the graph (and to prove I can mathematically do it) I don't have a problem with the maths side just the getting Matlab to display what I want.
I could probably do it quicker with pencil and paper but i want to get into the habit of using Matlab for plots as in the long run it will be more efficient, I just can't work out how to get matlab to display this maximum deviation ???
I inferred that ‘maximum non-linearity’ would be ‘maximum deviation’ between the two plots, but in which direction? This could become a relatively complicated problem involving perpendicularity from the linear plot (something my pencil-and-paper solutions intended to provide without formal proof, since I’m not up to that tonight). Or it could become something else entirely.
In the end, to provide the least complicated MATLAB solution, I decided to simply subtract one curve (linear) from the other (cubic), and calculated the difference. You didn’t describe the method you want to use, assuming it is different than the one I chose. There are likely several different — and equally valid — metrics. (I rely on Roger Stafford for those insights. He usually thinks of them more quickly and describes them more fully and precisely.)
The full MATLAB expression for the analytic solution I implemented previously on paper (and using your ‘y_1’ function) might be coded as:
y_1=@(x)+1+2*x+0.05*x.^2-0.00833*x.^3;
y_2 = @(x) 1 + 2.* x;
x = linspace(0,6);
dy = @(x) y_1(x) - y_2(x);
[maxd,idx] = max(dy(x));
xmax = x(idx);
This gives ‘maxd’ (max difference) and ‘xmax’ (the x-value at which ‘xmax’ occurs) as:
maxd =
266.8800e-003
xmax =
4.0000e+000
reassuringly close to the analytic values I derived earlier. Note that for such determinations, it is best to use the linspace function, since it produces a vector of specified length (the default is 100), with appropriately more precise interim function evaluations. (You could likely use the fminsearch function and a few additional lines of code, but I doubt it would significantly improve the accuracy of the calculation.)
As much as I like MATLAB and its computational flexibility, sometimes paper-and-pencil derivations (perhaps supplemented or replaced by the Symbolic Maths Toolbox implementations of the same concepts) are the easiest to implement, especially for a one-off calculation.
Finally, there is no inherent contradiction between using pencil-and-paper and using MATLAB for certain calculations. One is always a check on the other, and that, to me at least, is beneficial.
It’s GMT-7 here. We can continue this discussion on the morrow.
Sorry for my absence i have been distracted by work
Apologies for my vagueness and thankyou for your help and perseverance so far.
It is the maximum deviation vertically that is required.
As a bit of background, the X axis is an input scale and the Y axis is an output, so at input X=1 my linear plot shows an ideal output response (it is a sensor) and my poly shows an actual output response at an input of 1.
I would like matlab to tell me at what input i have my max deviation and what my max deviation value is across an input range of 0-6 but most importantly id like to illustrate this on the plot it produces.
My calculations have determined the max deviation to be at an input of 4 which supports yours.
Yes i can see your argument for pencil and paper and i am using Matlab for verification of the mathematical solution but primarily i chose to use it to provide me with the graph rather than plot it by hand, as i will no doubt be plotting similar problems throughout my studies i figured if i could get the code constructed for the first problem it would speed up the process considerably for the rest.
Could you help with the code for the Plotting and displaying of the max deviation ???
If you want to plot it, all you need to do is to add a plot call:
y_1=@(x)+1+2*x+0.05*x.^2-0.00833*x.^3;
y_2 = @(x) 1 + 2.* x;
x = linspace(0,6);
dy = @(x) y_1(x) - y_2(x);
figure(1)
plot(x, dy(x))
grid
xlabel('X')
ylabel('Deviation')
The calculated value ‘dy’ should be the ‘maximum vertical distance’, since the difference ‘dy’ is evaluated at the same values of ‘x’ for both ‘y_1’ and ‘y_2’. The analytic solution (that may differ slightly) will take the maximum of the difference in the two curves, first subtracting one from the other and then doing the evaluation, yielding the maximum absolute distance. The difference is whether you do the subtraction after evaluating the two functions (numeric) or before (analytic).

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Annotations 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by