简体   繁体   中英

Matlab: plot Cubic function for range of y values

I have a cubic equation:

roots([9E-10 -2E-06 0.0014 0.039])

I am trying to plot the equation for y values of 0.0 to 0.5 (which I know gives x values in the range of 0 to 700. (ie the equation has been fit to this data)

r=0.0039-y; %where y ranges from 0.0 to 0.5
[eqnroots]=roots([9E-10 -2E-06 0.0014 r])

I find the real root using

isreal(eqnroots(n));

and then plot the equation but it does not give the correct x/y range and fit looks wrongs.

The roots function only yields the roots of the polynomial equation. To generate all the y-values for a given set of x-values you need to use polyval .

Try this:

% Polynomial coefficients
p = [9E-10 -2E-06 0.0014 0.039];

% Generate y-values for x-range we are interested in
x = -270:0.1:1350;
y = polyval(p,x);

% Find roots of the polynomial.
% Select any where the imaginary component is negligible, i.e. it is real.
% As it's a root, the corresponding y-value will be 0.
xroot = roots(p);
xroot = xroot(abs(imag(xroot)) < 1e-10);
yroot = zeros(size(xroot));

% Plot the polynomial and its real roots.
figure;
hold on
plot(x,y, 'b')
plot(xroot,yroot, 'rx')

This gives the following plot:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM