Increment a vector in ODE45
Mostra commenti meno recenti
Hi everyone. I have to solve an equation using ODE45,in which i have a vector (15x1). How can i increment the vector position? I have: (the first one is an example)
function dydt= test(t,y,x)
ode1= k*A(x)+(y(1)-2) %A is the vector
end
in another file i have
tspan = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15];
y0 = [20];
for x=1:1:15
[t,y] = ode45(@(t,y) test(t,y,x),tspan,y0);
end
The problem is that i want A(1) in the first iteration where y(1) is y(0),A(2) where y(1) is the result of the last step...till A(15). How can i do it? I am sorry for my bad explanation and for my bad english. Thanks to everyone.
Risposte (1)
Jan
il 24 Ott 2018
tspan = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15];
y0 = 20;
tC = [];
yC = [];
for k = 1:14
[t,y] = ode45(@(t,y) test(t,y,k), tspan(k:k+1), y0);
tC = cat(1, tC, t); % Collect the total output
yC = cat(1, yC, y);
y0 = y(end); % Set new initial value
end
9 Commenti
Diego Dessi
il 24 Ott 2018
Diego Dessi
il 24 Ott 2018
Jan
il 25 Ott 2018
You did not post the definition of A. I guess, that the error is caused by A(k). You can check this easily using the debugger. Type this in the command window:
dbstop if error
Then run the code again until Matlab stops. Now check the sizes:
size(A)
k
What do you see?
Diego Dessi
il 25 Ott 2018
Diego Dessi
il 25 Ott 2018
Modificato: Diego Dessi
il 25 Ott 2018
Jan
il 25 Ott 2018
@Diego Dessi: The problem is still not clear. Is this the failing code:
function dydt= test(t,y,k)
ode1= k*A(k)+(y(1)-2) %A is the vector
? Then where is A defined? Where do you create dydt? What do you do with ode1?
Diego Dessi
il 25 Ott 2018
Modificato: Diego Dessi
il 25 Ott 2018
@Diego: The shown code fails, because "file.m" considers "file" to be a struct with the field "m". Please post the real code. If you call a function or script instead of "file.m", it should work. So if you still do have any problems, please post the real code and a copy of the error message.
I've posted already some code, which let the integration run piecewise in the specified intervals of tspan. Your code calls the integration 15 times and overwrite the result in each iteration. I do not understand, what this code should achieve.
Diego Dessi
il 26 Ott 2018
Categorie
Scopri di più su Ordinary Differential Equations in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!