Matrix dimensions must agree.
Show older comments
Hello,
I had a problem with this:
clear;clc
t0=0;
tf=5;
Emax=1e-9;
Emin=1e-10;
hmax=2e-1;
hmin=1e-3;
nmax=1e4;
f=@(u,t) -u+exp(-t);
x0=0;
wyn=zeros(1,nmax);
time=zeros(1,nmax);
t=t0;
wyn(1)=x0;
h=1e-2;
Es=zeros(1,nmax);
time(1)=t0;
i=1;
k=zeros(6,1);
W4=[25/216,1408/2565,2197/4104,1/5];
W5=[16/135,6656/12025,28561/56430,9/50,2/55];
while(t<tf && i<nmax)
if h<hmin
h=hmin;
end
if h>hmax
h=hmax;
end
k(1)=h*f(t,wyn(i));
k(2)=h*f(t+1/4*h,wyn(i)+1/4*k(1));
k(3)=h*f(t+3/8*h,wyn(i)+3/32*k(1)+9/32*k(2));
k(4)=h*f(t+12/13*h,wyn(i)+1932/2197*k(1)-7200/2197*k(2)+7296/2197*k(3));
k(5)=h*f(t+h,wyn(i)+439/216*k(1)-8*k(2)+3680/513*k(3)-845/4104*k(4));
k(6)=h*f(t+1/2*h,wyn(i)-8/27*k(1)+2*k(2)-3544/4104*k(3)+1856/4104*k(4)-11/40*k(5));
R4=wyn(i)+sum(W4.*k(1:end-1));
R5=wyn(i)+W5.*k;
Ek=abs(R5-R4); - here is a problem.
if Ek>Emax
h=1/2*h;
else
x(i+1)=R5;
time(i+1)=t;
if Ek<Emin
h=2*h;
end
t=t+h;
i=i+1;
end
end
wyn=x(1:i-1);
time=time(1:i-1);
In line 37 is some problem can someone help me what i did wrong here?
Thanks For help.
Accepted Answer
More Answers (1)
Pawel Kostyra
on 1 Apr 2019
Edited: Pawel Kostyra
on 1 Apr 2019
1 Comment
Adam Danz
on 1 Apr 2019
Fortunately your code isn't very long. I suggest going through your code line by line starting at the top. Compare the results of each line with the expected results.
If you're getting a matrix where you expect to get a single value, you might need to transpose a vector or put a dot in front of multiplication or division symbols.
Categories
Find more on Logical 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!