필터 지우기
필터 지우기

Loop-can you suggest another way to write the following loop?

조회 수: 1 (최근 30일)
B
B 2015년 3월 19일
댓글: Star Strider 2015년 3월 19일
Hello,
I'm trying to create an m.file to evaluate the polynomial P5(x)=a0+a1x+a2x^2+a3*x^3+a4*x^4+a5*x^5, where a is a vector [-120 274 -225 85 -15 1]. The following code is working but I'm a beginner and always like to know how ppl with experience might write the code. Many thanks for your cooperation so far..
---------------------------------------------------------------------------------------------------------
%polyfive.m syms x a=[-120 274 -225 85 -15 1]; p5old=0
for m=1:length(a) n=m-1; p5new=p5old+a(m)*x^(n); p5old=p5new; m=m+1 ; end disp(p5new)

채택된 답변

Star Strider
Star Strider 2015년 3월 19일
I wouldn’t write a loop at all. I would use polyval:
a=[-120 274 -225 85 -15 1];
x = linspace(0, 6);
y = polyval(fliplr(a), x);
figure(1)
plot(x, y)
grid
  댓글 수: 4
B
B 2015년 3월 19일
Thanks a lot :)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by