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

조회 수: 53 (최근 30일)
Ran
Ran 2023년 10월 12일
댓글: 慧敏 2024년 5월 10일
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.
慧敏
慧敏 2024년 5월 10일
请问最后是怎么解决的?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 循环及条件语句에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!