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 个)
Star Strider
2015-1-2
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 个评论
Matthew
2015-1-3
Star Strider
2015-1-3
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.
Star Strider
2015-1-10
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!