필터 지우기
필터 지우기

How can I fix this error?

조회 수: 2 (최근 30일)
Andrew Williams
Andrew Williams 2018년 9월 5일
답변: Naman Chaturvedi 2018년 9월 10일
"Unable to perform assignment because the left and right sides have a different number of elements."
I'm trying to write a code that will approximate sin(0.3*pi) using a Taylor series expansion to 20 terms. What I currently have is:
a=0; x=0.3*pi; n=0; f(1)=0; y=sin(a);
for i=[1:20]
f(i+1)=(y*((x-a)^n))/factorial(n) y=diff(y); n=n+1;
end
Any advice or ways to help fix this problem would be greatly appreciated, I'm a novice still at using MATLAB.
  댓글 수: 1
Andrew Williams
Andrew Williams 2018년 9월 5일
or better yet, how can i get my program to take successive derivatives in a loop?

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

답변 (1개)

Naman Chaturvedi
Naman Chaturvedi 2018년 9월 10일
You are getting the error because after performing 'y=diff(y)', y becomes '[]' which makes the RHS '[]' as well. The correct code to find the expansion would be:-
n = 20;
x = pi/3;
y = zeros(1,20);
for i = 0:n
y(i+1) = (-1)^i*x^(2*i+1)/factorial(2*i+1);
end
approx_sinx=sum(y); %Approximated value

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by