Hi everyone, I hope you all are doing very well. I have been working on Matlab about taylor series recently, however, I usually get an error about matrix dimensions. Line 17 which starts with y1=y0 + ... . So can anybody help me out? thank you from now!
clc;
clear;
clear all;
x= -2:0.5:2;
dx=0.5;
yy=sin(x);
a=1;
b = diff(yy)/dx;
b1= diff(diff(yy)/dx)/dx;
b2= diff(diff(diff(yy)/dx)/dx)/dx;
b3= diff(diff(diff(diff(yy)/dx)/dx)/dx)/dx;
b4= diff(diff(diff(diff(diff(yy)/dx)/dx)/dx)/dx)/dx;
b5= diff(diff(diff(diff(diff(diff(yy)/dx)/dx)/dx)/dx)/dx)/dx;
b6= diff(diff(diff(diff(diff(diff(diff(yy)/dx)/dx)/dx)/dx)/dx)/dx)/dx;
b7= diff(diff(diff(diff(diff(diff(diff(diff(yy)/dx)/dx)/dx)/dx)/dx)/dx)/dx)/dx;
y0= sin(a);
y1= y0 + b.*(x-a);
y2= y0 + b.*(x-a) + b1/factorial(2).*((x-a)^2);
y3= y0 + b.*(x-a) + b1/factorial(2).*((x-a)^2) + b2/factorial(3).*((x-a)^3);
y4= y0 + b.*(x-a) + b1/factorial(2).*((x-a)^2) + b2/factorial(3).*((x-a)^3) + b3/factorial(4).*((x-a)^4);
y5= y0 + b.*(x-a) + b1/factorial(2).*((x-a)^2) + b2/factorial(3).*((x-a)^3) + b3/factorial(4).*((x-a)^4) + b4/factorial(5).*((x-a)^5);
y6= y0 + b.*(x-a) + b1/factorial(2).*((x-a)^2) + b2/factorial(3).*((x-a)^3) + b3/factorial(4).*((x-a)^4) + b4/factorial(5).*((x-a)^5) + b5/factorial(6).*((x-a)^6);
y7= y0 + b.*(x-a) + b1/factorial(2).*((x-a)^2) + b2/factorial(3).*((x-a)^3) + b3/factorial(4).*((x-a)^4) + b4/factorial(5).*((x-a)^5) + b5/factorial(6).*((x-a)^6) + b6/factorial(7).*((x-a)^7);
y8= y0 + b.*(x-a) + b1/factorial(2).*((x-a)^2) + b2/factorial(3).*((x-a)^3) + b3/factorial(4).*((x-a)^4) + b4/factorial(5).*((x-a)^5) + b5/factorial(6).*((x-a)^6) + b6/factorial(7).*((x-a)^7) + b7/factorial(8).*((x-a)^8);

댓글 수: 8

You are not taking a true derivative, but you are calculating the difference between each array element.
Also, you could make your code much more readable by doing something like this:
b0 = diff(yy)/dx;
b1= diff(b)/dx;
b2= diff(b1)/dx;
b3= diff(b2)/dx;
b4= diff(b3)/dx;
b5= diff(b4)/dx;
b6= diff(b5)/dx;
b7= diff(b6)/dx;
By not using numbered variables, but using a cell array instead, you can further simplify the code:
b=cell(1,8);
b{1}=diff(yy)/dx;
y=cell(1,numel(b));
y{1}=sin(a);
y{2}=y{1}+b{1}*(x-a)
for n=2:numel(b)
b{n}=diff(b{n-1})/dx;
y{n+1}=y{n} + b{n}*((x-a)^n)/factorial(n);
end
Note that Matlab is one-indexed, meaning that y0 becomes y{1}.
If you use the symbolic toolbox to generate the variable x, this should work as intended. For a numerical fix, you will have to think about how you want to extend you vector after you apply diff.
Onur Totos
Onur Totos 2019년 1월 7일
Thank you for your help! It really assisted me to learn about how to make it short and clear. If you were in my shoes, how would you write this example on the matlab?
I think I would take the mid-points of the x-vector as well when doing a diff for the b.
So then you would have
x_new=x_old(2:end)-dx/2;
That would enable you to continue the loop not to an arbitrary point, but to the point where you don't have a long enough x.
Also, you should change that first assingment to this:
dx=0.5;
x=-2:dx:2;
The other route takes you a bit away from numerical approximation: symbolic variables.
Onur Totos
Onur Totos 2019년 1월 8일
Thanks for responding to me. I appreciate your help! The problem is still going on the program... Still I am having a problem about matrix dimensions and line 20.. Anyways, thanks again..!!
Rik
Rik 2019년 1월 8일
What is the code you are currently using?
Onur Totos
Onur Totos 2019년 1월 8일
편집: Rik 2019년 1월 8일
clc; clear ;clear all;
dx= 0.5;
x= -2:dx:2;
yy=sin(x);
a=1;
b = diff(yy)/dx;
b1= diff(b)/dx;
b2= diff(b1)/dx;
b3= diff(b2)/dx;
b4= diff(b3)/dx;
b5= diff(b4)/dx;
b6= diff(b5)/dx;
b7= diff(b6)/dx;
y0= sin(a);
y1= y0 + b.*(x-a);
y2= y1 + b1/factorial(2).*((x-a)^2);
y3= y2 + b2/factorial(3).*((x-a)^3);
y4= y3 + b3/factorial(4).*((x-a)^4);
y5= y4 + b4/factorial(5).*((x-a)^5);
y6= y5 + b5/factorial(6).*((x-a)^6);
y7= y6 + b6/factorial(7).*((x-a)^7);
y8= y7 + b7/factorial(8).*((x-a)^8);
I am trying to use taylor series for sin(x)
Jan
Jan 2019년 1월 8일
@Onur Totos: Please use the code style to improve the readability of your code in the forum.
Rik
Rik 2019년 1월 8일
There are a few problems here:
  1. you insist on numbered variables
  2. you did not account for diff changing the size of your vector
  3. you are still using clear all, instead of clear variables

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

 채택된 답변

nanren888
nanren888 2019년 1월 7일
편집: per isakson 2019년 1월 7일

0 개 추천

>> whos
Name Size Bytes Class Attributes
a 1x1 8 double
b 1x8 64 double
b1 1x7 56 double
b2 1x6 48 double
b3 1x5 40 double
b4 1x4 32 double
b5 1x3 24 double
b6 1x2 16 double
b7 1x1 8 double
dx 1x1 8 double
x 1x9 72 double
yy 1x9 72 double
The result of diff is shorter, being always the difference between elements.
sometimes;
something = [0,diff(somethingElse)]
can be useful.

댓글 수: 1

Onur Totos
Onur Totos 2019년 1월 7일
Thank you very much for your respons. It makes sense. However, I am tired of dealing with this problem since the morning. So what exactly do I have to do for fixing this problem? Regards

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

태그

질문:

2019년 1월 7일

댓글:

Rik
2019년 1월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by