简体   繁体   English

Matlab:为y值范围绘制三次函数

[英]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) 我正在尝试绘制0.0到0.5的y值的方程(我知道x值在0到700的范围内。(即,方程已经适合此数据)

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. 然后绘制方程式,但它没有给出正确的x / y范围,并且拟合看起来是错误的。

The roots function only yields the roots of the polynomial equation. roots函数仅产生多项式方程的根。 To generate all the y-values for a given set of x-values you need to use polyval . 要为给定的一组x值生成所有y值,您需要使用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: 这给出了以下图:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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