Matrix dimensions must agree.
13 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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.
0 commentaires
Réponse acceptée
Adam Danz
le 1 Avr 2019
At line 37 (the line that contains Ek=abs(R5-R4)), the variable R5 is a [6x5] matrix while R4 is [1x4]. When subtracting matrices, the dimensions must agree (as the error message indicates).
It would take quite a bit of effort to reverse engineer your code to figure out what it's supposed to be doing and then suggest a correction. I can more quickly identify potential sources of the problem.
The 2 lines below produce two different sized vectors.
W4=[25/216,1408/2565,2197/4104,1/5]; % [1 x 4]
W5=[16/135,6656/12025,28561/56430,9/50,2/55]; % [1 x 5]
Since 'k' is a [6 x 1] vector, when you multiply k by W5, you produce a [6 x 5] matrix.
The [1 x 4] vector stored in R4 is a result of this line: sum(W4.*k(1:end-1)). Here, W4 is [1 x 4] while k is [6 x 1] and the result of the multiplication is [5 x 4] but then you're taking the sum over the 2nd dimension making it [1 x 4].
Long story short, there's probaby a vector with the wrong orientation (column vs row) or perhaps the W4 and W5 vectors should have the same length. You'll need to sift through the code, line by line, to make sense of it.
0 commentaires
Plus de réponses (1)
Pawel Kostyra
le 1 Avr 2019
Modifié(e) : Pawel Kostyra
le 1 Avr 2019
1 commentaire
Adam Danz
le 1 Avr 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.
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!