0

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. (i.e. 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.

1 Answer 1

3

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:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.