Error in ode45: issue with length of initial conditions vector
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hey all, I was trying to solve the differential equation dUp/dt = c(U-Up) using ode45 in MATLAB.
I have U(A,x,y,t) as a user defined function. I then created the odefun file as follows:
function dydt = odefun(t,up,x,y,A,c,E)
global A c x y E
dydt = c.*(u(A,x,y,t) - up);
end
I then tried to calculate and plot Up vs t. The code is as follows:
A = 1.2;
E = 0.1;
c = 0.1;
x = 1;
y = 0;
tspan = [0 1];
y0 = 0;
[t,up] = ode45(@(t,up)odefun(t,up,x,y,A,c,E), tspan, y0);
plot(t,up)
I keep getting this error:
Error using odearguments
@(T,UP)ODEFUN(T,UP,X,Y,A,C,E) returns a vector of length 0, but the length of initial conditions vector is 1. The vector
returned by @(T,UP)ODEFUN(T,UP,X,Y,A,C,E) and the initial conditions vector must have the same number of elements.
I have seen some community posts that have addressed this issue, but those are generally for a system of equation. I am unable to figure out what the issue is in this case. Please help.
0 Kommentare
Antworten (1)
Star Strider
am 4 Sep. 2024
The ‘u’ function is either not being called correctly or not written correctly. In any event, it is miissing in the posted code.
Supplying a ‘u’ that is reasonable works —
A = 1.2;
E = 0.1;
c = 0.1;
x = 1;
y = 0;
tspan = [0 1];
y0 = 0;
[t,up] = ode45(@(t,up)odefun(t,up,x,y,A,c,E), tspan, y0);
plot(t,up)
function dydt = odefun(t,up,x,y,A,c,E)
u = @(A, x, y, t) A*x*y+t; % Create 'u' (Not Posted)
dydt = c.*(u(A,x,y,t) - up);
end
.
5 Kommentare
Sam Chak
am 4 Sep. 2024
I do not understand how the function u can be both unbounded and perfectly bounded at the same time. The adjective "bounded" is a mathematical property and does not imply that the function u has finite values under certain conditions. As t approaches specific values, if the function can take on arbitrarily large values, then it is unbounded.
Perhaps you could consider posting the entire function u here and exploring how to use the Events feature in the ODE solver, as advised by @Star Strider.
Siehe auch
Kategorien
Mehr zu Ordinary Differential Equations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!