Filter löschen
Filter löschen

error in ode45. I have declared the vector to be 3x1 but it only reads it as 2x1.

2 Ansichten (letzte 30 Tage)
for kpm=27:1:30
tspan = 0:30;
yo=[0.01 0.05 0.179];
[t,y]=ode45(@newwaykumar2004,tspan,yo);
end
function dydt=newwaykumar2004(t,y)
kp=3;
global kpm;
kmp = 25;
klm = 15;
kl = 1;
theta=1;
w=0.5;
function qz=f(m)
qz=1+tanh((m-theta)/w);
end
dpdt=kp*y(1)*(1-y(1))-kpm*y(1)*y(2);
dmdt=(kmp*y(1)+y(3))*y(2)*(1-y(2))-y(2);
dIdt=klm*f(y(2))-kl*y(3);
dydt=[dpdt;dmdt;dIdt];
end

Akzeptierte Antwort

Dyuman Joshi
Dyuman Joshi am 21 Apr. 2023
Bearbeitet: Dyuman Joshi am 21 Apr. 2023
You get the error because dpdt is empty, and thus dydt is 2x1.
Instead of defining kpm as global, it is better to define it as an input to the ode function. MathWorks recommends to avoid the use of global as well
Note that in your code t and y are being overwritten in each iteration of the loop. I have modified t and y to store the result of each iteration
%Defining constant value variables outside the loop
kpm = 27:1:30;
tspan = 0:30;
yo=[0.01 0.05 0.179];
n = numel(kpm);
nt = numel(tspan);
nyo = numel(yo);
%Pre-allocation
t = zeros(nt,n);
y = zeros(nt,nyo,n);
%Ode Loop
for k = 1:n
[t(:,k),y(:,:,k)]=ode45(@(t,y) newwaykumar2004(t,y,kpm(k)), tspan, yo);
end
%t(:,k) represents a solution corresponding to kpm(k)
t
t = 31×4
0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 8 9 9 9 9
%y(:,:,k) represents a solution corresponding to kpm(k)
y
y =
y(:,:,1) = 0.0100 0.0500 0.1790 0.0561 0.0489 0.4715 0.0591 0.1776 0.7081 0.0044 0.2052 1.0206 0.0005 0.1854 1.0996 0.0001 0.1686 1.0806 0.0000 0.1508 1.0303 0.0000 0.1309 0.9670 0.0000 0.1094 0.8979 0.0000 0.0876 0.8284 0.0000 0.0672 0.7634 0.0001 0.0494 0.7067 0.0007 0.0352 0.6604 0.0062 0.0256 0.6254 0.0580 0.0305 0.6074 0.0759 0.2007 0.7492 0.0023 0.2373 1.1380 0.0001 0.2241 1.2470 0.0000 0.2198 1.2647 0.0000 0.2172 1.2619 -0.0000 0.2142 1.2526 -0.0000 0.2103 1.2386 0.0000 0.2049 1.2194 -0.0000 0.1976 1.1938 0.0000 0.1881 1.1602 0.0000 0.1757 1.1176 0.0000 0.1603 1.0652 0.0000 0.1420 1.0041 0.0000 0.1214 0.9359 0.0000 0.0995 0.8667 0.0000 0.0781 0.7985 y(:,:,2) = 0.0100 0.0500 0.1790 0.0540 0.0481 0.4711 0.0586 0.1675 0.6959 0.0048 0.1943 0.9850 0.0006 0.1727 1.0543 0.0001 0.1533 1.0280 0.0000 0.1331 0.9714 0.0000 0.1116 0.9043 0.0000 0.0898 0.8350 0.0001 0.0692 0.7696 0.0003 0.0512 0.7121 0.0016 0.0370 0.6651 0.0131 0.0291 0.6308 0.0879 0.0538 0.6258 0.0291 0.2158 0.8615 0.0013 0.2077 1.1050 0.0001 0.1911 1.1424 0.0000 0.1772 1.1161 0.0000 0.1616 1.0681 0.0000 0.1434 1.0083 0.0000 0.1228 0.9411 0.0000 0.1010 0.8711 0.0000 0.0795 0.8027 0.0000 0.0600 0.7402 0.0000 0.0434 0.6877 0.0000 0.0303 0.6451 0.0004 0.0206 0.6127 0.0048 0.0143 0.5895 0.0617 0.0163 0.5771 0.0989 0.2203 0.7202 0.0012 0.2602 1.2164 y(:,:,3) = 0.0100 0.0500 0.1790 0.0521 0.0474 0.4707 0.0589 0.1581 0.6842 0.0053 0.1853 0.9552 0.0007 0.1627 1.0185 0.0002 0.1416 0.9882 0.0001 0.1202 0.9288 0.0001 0.0983 0.8610 0.0001 0.0770 0.7941 0.0003 0.0580 0.7335 0.0013 0.0424 0.6825 0.0092 0.0325 0.6439 0.0625 0.0441 0.6286 0.0493 0.1828 0.7753 0.0029 0.1959 1.0286 0.0003 0.1750 1.0753 0.0000 0.1566 1.0430 0.0000 0.1370 0.9854 0.0000 0.1158 0.9181 0.0000 0.0939 0.8483 0.0000 0.0729 0.7816 0.0000 0.0542 0.7222 0.0002 0.0388 0.6726 0.0014 0.0272 0.6338 0.0144 0.0210 0.6064 0.1126 0.0487 0.6057 0.0208 0.2327 0.8997 0.0006 0.2146 1.1448 0.0000 0.2005 1.1806 0.0000 0.1896 1.1607 0.0000 0.1773 1.1218 y(:,:,4) = 0.0100 0.0500 0.1790 0.0502 0.0467 0.4704 0.0591 0.1494 0.6738 0.0057 0.1771 0.9284 0.0008 0.1537 0.9870 0.0002 0.1316 0.9542 0.0001 0.1096 0.8938 0.0001 0.0878 0.8271 0.0002 0.0675 0.7633 0.0008 0.0501 0.7074 0.0046 0.0373 0.6627 0.0312 0.0359 0.6347 0.0848 0.1121 0.6774 0.0094 0.1901 0.9267 0.0008 0.1688 1.0242 0.0001 0.1477 1.0048 0.0000 0.1266 0.9487 0.0000 0.1047 0.8816 0.0000 0.0831 0.8134 0.0001 0.0631 0.7502 0.0003 0.0461 0.6958 0.0020 0.0331 0.6522 0.0163 0.0266 0.6213 0.0999 0.0596 0.6232 0.0202 0.2071 0.8726 0.0010 0.1896 1.0614 0.0001 0.1696 1.0720 0.0000 0.1511 1.0285 0.0000 0.1311 0.9668 0.0000 0.1095 0.8980 0.0000 0.0877 0.8286
function dydt=newwaykumar2004(t,y,kpm)
kp=3;
kmp = 25;
klm = 15;
kl = 1;
theta=1;
w=0.5;
%function handle
f = @(m) 1+tanh((m-theta)/w);
dpdt=kp*y(1)*(1-y(1))-kpm*y(1)*y(2);
dmdt=(kmp*y(1)+y(3))*y(2)*(1-y(2))-y(2);
dIdt=klm*f(y(2))-kl*y(3);
dydt=[dpdt;dmdt;dIdt];
end
  1 Kommentar
Stephen23
Stephen23 am 21 Apr. 2023
Bearbeitet: Stephen23 am 21 Apr. 2023
Ayush Ranjan: you should follow Dyuman Joshi's excellent advice to make KPM an input argument (and avoid GLOBAL variables). Note that the ODE45 documentation includes an example showing how to pass extra parameters to the ODE function:
which links in turn to this page:

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by