matrix dimensions must agree.
1 view (last 30 days)
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
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
nanren888
on 7 Jan 2019
Edited: per isakson
on 7 Jan 2019
>> 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.
More Answers (0)
See Also
Categories
Find more on Debugging and Analysis 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!