matrix dimensions must agree.

조회 수: 1 (최근 30일)
Onur Totos
Onur Totos 2019년 1월 7일
댓글: Rik 2019년 1월 8일
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에서 Debugging and Analysis에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by