Solving using an ode
3 views (last 30 days)
Show older comments
I am new to matlab and have not been able to figure out this question. I have to use the function 'ode45' do derive dx/dt=q*x^(s)+w*x.
some info:
0<=t<=20
the initial value of x=0.2.
q=10
w=3
s=[0.60 0.64 0.68 0.72 0.76 0.80]
t1=[5.34 4.25 3.25 2.12 1.96 1.52]
the question asks to solve the equation using ode45 function and use the solution to find the time t it would take for x =10 for every value of s
Here is the code I have, I know it is wrong but i thought I would just have it here.
s=[0.60 0.64 0.68 0.72 0.76 0.80];
t1=[5.34 4.25 3.25 2.12 1.96 1.52];
q=10;
w=3;
x0=0.2;
x1=10;
ode@(t1,x)(q*x^s+w*x);
[t1,x]=ode45(@ode,[0:20],0.2);
0 Comments
Answers (2)
Bjorn Gustavsson
on 28 Aug 2020
You want to integrate your ode for a couple of values of s. To do that you can either create one ode-function for all 6 values of s or you could do the ode-integration for one value of s at a time. When it comes to integrating multiple uncoupled ODEs it might take longer time to call ode45 with them all at once because ode45 uses adptive time-steps - and the separate values of s might lead to shorter steps in different regions of your time-variable, on the other hand it might also be quicker. If you do the integration of the odes one-by one you will have to create the variable ode once for each value of s, perhaps in a loop and save the results somehow. Once you've made that work you should also have a look at the way odeNN handles "events" that would make it easier to achieve the goal for you.
HTH
0 Comments
Alan Stevens
on 28 Aug 2020
Edited: madhan ravi
on 28 Aug 2020
Here's a "quick and dirty" way to do it, using interpolation to find the time.
s = [0.60, 0.64, 0.68, 0.72, 0.76, 0.80];
x0 = 0.2;
x1 = 10;
tf = zeros(1, numel(s));
tend = 0.4;
for k = 1 : numel(s)
[t, x] = ode45(@odefn, [0, tend], x0, [], s(k));
tf(k) = interp1(x, t, x1);
plot(t, x)
hold on
end
plot([0, tend], [x1, x1], 'r')
xlabel('t')
ylabel('x')
grid
text(0.01, 20, sprintf('tf = %.2f\n', tf))
function dxdt = odefn(~, x, s)
q = 10;
w = 3;
dxdt = q * x ^ s + w * x;
end
1 Comment
Alan Stevens
on 28 Aug 2020
Edited: Alan Stevens
on 28 Aug 2020
A slightly cleaner looking version madhan, though sprintf could do with extra spaces for clarity on the graph!.
That's better!
See Also
Categories
Find more on Ordinary Differential Equations 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!