matrix dimensions must agree.
Show older comments
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 Comments
Rik
on 7 Jan 2019
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
on 7 Jan 2019
Rik
on 7 Jan 2019
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
on 8 Jan 2019
Rik
on 8 Jan 2019
What is the code you are currently using?
Onur Totos
on 8 Jan 2019
Edited: Rik
on 8 Jan 2019
Jan
on 8 Jan 2019
@Onur Totos: Please use the code style to improve the readability of your code in the forum.
Rik
on 8 Jan 2019
There are a few problems here:
- you insist on numbered variables
- you did not account for diff changing the size of your vector
- you are still using clear all, instead of clear variables
Accepted Answer
More Answers (0)
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!