matrix dimensions must agree.

1 次查看(过去 30 天)
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 个评论
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
>> 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 个)

类别

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