matrix dimensions must agree error

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

采纳的回答

Matt J
Matt J 2020-5-24
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));
^
|

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by