Question about using ODE45

8 Ansichten (letzte 30 Tage)
Nicia Nanami
Nicia Nanami am 4 Okt. 2017
Bearbeitet: Jan am 10 Okt. 2017
I have this equation: dy/dx= 1/(a*x^-3+b*x^-5)
a=(3/4)t
b=(1/2)t^2
t=[0.5 20]
x=[2 5]
The question is using ODE45 to solve that equation for every value of t
This my function file:
function [dydx] = Myfun( x,y,t )
dydx=1/(((3/4.*t).*x^(-3))+((1/2).*t^2).*x^(-5))
end
My script file:
t=linspace (0.5,20,10);
options=optimset('Display','Off');
for in = 1:length(t)
T1=t(in)
[x,y,x1,y1,i1]=ode45(@(x,y)Myfunc(x,y,t(in)),[2 5],0,options);
xn=x1(:,1)
u(in)=x1(:,1);
end
For some reason, my script doesn't run and it shows error. Please kindly help me to check what the problem is.
Thanks in advance

Antworten (3)

Star Strider
Star Strider am 4 Okt. 2017
You need to vectorise ‘Myfunc’. You also need to check the initial condition, since a zero initial condition results in a uniformly zero integrated result.
Try this variation on your code:
Myfunc = @(t,x) 1./(((3/4.*t).*x.^(-3))+((1/2).*t.^2).*x.^(-5));
t = linspace (0.5,20,10);
options=optimset('Display','Off');
[T,X] = ode45(Myfunc, t, 1E-3, options);
figure(1)
plot(T, X)
grid on
  11 Kommentare
Nicia Nanami
Nicia Nanami am 6 Okt. 2017
Thank you for your help, I appreciate it.
Star Strider
Star Strider am 6 Okt. 2017
As always, my pleasure!

Melden Sie sich an, um zu kommentieren.


Roger Stafford
Roger Stafford am 4 Okt. 2017
Bearbeitet: Roger Stafford am 4 Okt. 2017
(Corrected)
Using ‘ode45’ is a very bad idea for this problem. Since dy/dx does not depend on y, this is a simple problem in integration for which you can use Matlab’s ‘int’ function in the Symbolic Toolbox. You can get a solution from ‘int’ in terms of 'a' and ‘b’, taking into account your y value of zero for x equal to 2. All you have to do then is substitute in the respective values 3/4*t and 1/2*t^2 and you have a general expression for y in terms of x and t.
  2 Kommentare
Nicia Nanami
Nicia Nanami am 5 Okt. 2017
Thank you for your respond but the question asks to use ode45 to solve this problem so I'm allowed to use other technique to solve it.
Jan
Jan am 6 Okt. 2017
Bearbeitet: Jan am 6 Okt. 2017
@Nicia: I assume this is a homework of a lesson for numerical mathematics. Then Roger's argument is essential (+1), because actually the instructions should teach you how to use numerics properly. Using ODE45 to integrate this is a poor approach. I have seen too many works of students and PhD theses, which used the wrong numerical method with the rationale, that they "produce a result". It is like using a hammer to push a screw into to wood.
But this is a side note only. If your professor asks for using ODE45, this is his mistake, but you should use it for this homework.

Melden Sie sich an, um zu kommentieren.


John BG
John BG am 5 Okt. 2017
Hi Nicia
1.
t is just a parameter, not the reference vector of the differential equation.
the output of ode45() is not
[T,X]=ode45(..)
but
[X,Y]=ode45(..)
.
2.
There are other more efficient ways to use ode45, but a way to have it running is
clear all;clc;close all;
x=[2:.01:5];
t=[-100 .5 20 1e3];
options=optimset('Display','off');
x_span=[2 5];
figure(1); hold all
for k=1:1:numel(t)
t1=t(k);
f1=@(t1,x) 1/((.75*t1)*x^-3+(.5*t1^2)*x^-5);
[X,Y]=ode45(f1,x_span,0.7,options)
hold all; plot(X,Y)
end
grid on
.
3.
Perhaps a 'better picture' of the beahviour is obtained when expanding the range
x=[-20:.01:50];
x_span=[-20 50];
figure(2); hold all
for k=1:1:numel(t)
t1=t(k);
f1=@(t1,x) 1/((.75*t1)*x^-3+(.5*t1^2)*x^-5);
[X,Y]=ode45(f1,x_span,0.7,options)
hold all; plot(X,Y)
end
grid on
.
4.
Also, integration required, because of the dydx, than then would be
clear all;clc;close all;
x=[2:.01:5];
t=[-100 .5 20 1e3];
options=optimset('Display','off');
x_span=[2 5];
figure(1); hold all
for k=1:1:numel(t)
t1=t(k);
f1=@(t1,x) 1/((.75*t1)*x^-3+(.5*t1^2)*x^-5);
[X,Y]=ode45(f1,x_span,0.7,options)
hold all; plot(X,cumsmum(Y))
end
grid on
5.
and integrating the wider range
x=[-20:.01:50];
x_span=[-20 50];
figure(2); hold all
for k=1:1:numel(t)
t1=t(k);
f1=@(t1,x) 1/((.75*t1)*x^-3+(.5*t1^2)*x^-5);
[X,Y]=ode45(f1,x_span,0.7,options)
hold all; plot(X,cumsum(Y))
end
grid on
.
.
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG
  4 Kommentare
John BG
John BG am 6 Okt. 2017
Bearbeitet: John BG am 6 Okt. 2017
Hi again Nicia
ok, please confirm; you need t(y), not y(t) right?
would it be possible to broadly know what are you modelling, a rupture?
John BG
John BG am 7 Okt. 2017
Bearbeitet: John BG am 7 Okt. 2017
Jan Simon, would you please abstain from adding any comment to my question to Nicia t(y) or y(t)? at least until Nicia says something?
Nicia found my answer helful, what was the point of you saying not useful? it's not your question aber Nicia's.
If Nicia wishing to do so, please let Nicia follow up right after my question.
And Jan, you didn't span the range to see the step, I did show the step. Spotting when it happens is probably the objective of the question.
Again, To Nicia:
Hi again Nicia
do you need t(y) or y(t)?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by