Time-based integration of ODEs
2 views (last 30 days)
Show older comments
Commented: Ali Almakhmari on 13 Jan 2023
Hello everyone. I have a problem that I hope I can get some help on. So I have the following set of differential equations:
I need to solve for θ, ϕ, and ψ from those three differential equations. To be specific I need to solve for a value (not a function) of θ, ϕ, and ψ at a given moment. I have a for loop that runs, and at each iteration the value of p, q, and r change, so at each iteration I will need to do this integration and solve for a value of θ, ϕ, and ψ. What information do I have avaliable for me? The value of p, q, and r at each iteration, and I also know the initial values (iteration = 1) of θ, ϕ, and ψ, and finally, I know the time at each iteration as well. Is this possible in MATLAB? I tried implementing this with ODE45 and struggled very much. Any help is super appreciated.
Steven Lord on 13 Jan 2023
The problem you've written out looks quite similar to the "Pass Extra Parameters to ODE Function" example on the ode45 documentation page. You have a system of three first-order ODEs rather than the system of two first-order ODEs given in the example and you have three extra parameters (p, q, and r) rather than the two (A and B) from the example, but those will require only a small modification to the odefun and to the ode45 call.
The fact that p, q, and r are functions of time rather than fixed values is a little bit more challenging, but the "ODE with Time-Dependent Terms" example on that same documentation page shows how you can handle them if you have a vector of data. If you have function handles for p, q, and r that's even easier.
f = @(t) t.^2;
[t, y] = ode45(@(t, y) myode(t, y, f), [0 1], 1);
Let's check the symbolic solution.
syms ts ys(ts) % Using ts and ys instead of t and y so I can use t and y for plotting below
solution = dsolve(diff(ys, ts) == ts^2*ys(ts), ys(0)==1)
And now plot the numeric solution and the value of the symbolic solution at the same times.
plot(t, y, '+', t, subs(solution, ts, t), 'o')
Those look to be in pretty good agreement.
function dydt = myode(t, y, f)
fvalue = f(t);
dydt = fvalue*y; % dydt = t^2*y
More Answers (0)
Find more on Programming 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!