Clear Filters
Clear Filters

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

90 views (last 30 days)
Ran
Ran on 12 Oct 2023
Commented: 慧敏 on 10 May 2024
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);

Accepted Answer

Yash
Yash on 18 Oct 2023
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 Comments
Ran
Ran on 19 Oct 2023
Thank you very much!It helps me a lot and solves my homework i have been struggling with.
慧敏
慧敏 on 10 May 2024
请问最后是怎么解决的?

Sign in to comment.

More Answers (0)

Categories

Find more on 安装和许可简介 in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!