MATLAB Answers

matrix dimensions must agree error

2 views (last 30 days)
Alicia G
Alicia G on 24 May 2020
Commented: Alicia G on 24 May 2020
Hello guys, I am trying to do this problem, but ı get matrix dimensions must agree error. Can you please help me with that?
a=0;
b=4;
ca=1;
cb=-3;
a1=@(x) 0.*x;
a0=@(x) (1+x);
g=@(x) 1-x.^2;
h=0.01;
N=round((b-a)/h);
x=linspace(a,b,N+1);
x=x';
G=g(x);
K=1/h^2*(2.*diag(ones(1,N+1))-diag(ones(1,N)-1)-diag(ones(1,N),1)); % heres the error 'Matrix dimensions must agree.'
A=diag(a0(x));
A(1,1)=0;
A(end,end)=0;
A1=a1(x);
B=1/(2*h)*(diag(A1(1:end-1),1)-diag(A1(2:end),-1));
B(1,2)=0;
B(end,end-1)=0;
G(1)=ca;
G(end)=cb;
K(1,1)=1;
K(1,2)=0;
K(end,end-1)=0;
K(endd,end)=1;
y=(K+B+A)\G;

  0 Comments

Sign in to comment.

Accepted Answer

Matt J
Matt J on 24 May 2020
Possibly a missing comma here,
K=1/h^2*(2.*diag(ones(1,N+1))-diag(ones(1,N)-1)-diag(ones(1,N),1));
^
|

  2 Comments

Matt J
Matt J on 24 May 2020
Also, a typo in this line
K(endd,end)=1;
Alicia G
Alicia G on 24 May 2020
thanks!! i didn't see that :)

Sign in to comment.

More Answers (0)


Translated by