函数或变量 'poly2str' 无法识别,如何导入poly2str?

x=-2*pi:pi/2:2*pi;
y=[1,0,-1,0,1,0,-1,0,1];
p2=polyfit(x,y,2);
p3=polyfit(x,y,3);
p4=polyfit(x,y,4);
p6=polyfit(x,y,6);
disp('二次拟合函数'),f2=poly2str(p2,'x');
disp('三次拟合函数'),f3=poly2str(p3,'x');
disp('四次拟合函数'),f4=poly2str(p4,'x');
disp('七次拟合函数'),f6=poly2str(p6,'x');
x1=-2*pi:pi/10:2*pi;
y2=polyval(p2,x1);
y3=polyval(p3,x1);
y4=polyval(p4,x1);
y6=polyval(p6,x1);
yy=cos(x1);
plot(x,y,x1,y2,'-b',x1,y3,'-.r',x1,y4,'*c',x1,y6,'.g',x1,yy);

 采纳的回答

Yash
Yash 2023-10-18
Hi Ran,
我会用英语回答
I understand that you want to use "poly2str" function to return polynomial as string. I tried reproducing the error "Unrecognized function or variable 'poly2str'" on my end. However, I didn't encounter any error. Consider running the following command in the MATLAB command window to check if "poly2str.m" is present on the root path:
>> which poly2str -all
If "poly2str.m" is not found by "which", then consider running the following command in the MATLAB command window to refresh function and file system path cache:
>> rehash toolboxcache
If "poly2str.m" is still not found by "which", then verify if "Control System" toolbox is installed or not. Refer here for more information on installing a toolbox: https://www.mathworks.com/matlabcentral/answers/101885-how-do-i-install-additional-toolboxes-into-an-existing-installation-of-matlab
Alternatively, consider using the function "poly2sym" to create symbolic polynomial from vector of coefficients. Refer here for more information on "poly2sym" function: https://www.mathworks.com/help/releases/R2023a/symbolic/sym.poly2sym.html
>> p = poly2sym([0.75, -0.5, 0.25])
p =
(3*x^2)/4 - x/2 + 1/4
I hope this helps!

2 个评论

Ran
Ran 2023-10-19
Thank you very much!It helps me a lot and solves my homework i have been struggling with.
请问最后是怎么解决的?

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 安装和许可简介 的更多信息

产品

版本

R2023a

Community Treasure Hunt

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

Start Hunting!