Hello , I am trying to solve an ode in matlab
but i'm getting syntax errors. pls help!!
my actual ODE is : m*d2xdt2 + a*(dxdt)^2 + k*x= Fcos(omega*t)
and i need a solution for x which is displacement for an object.
i am trying this way please correct me where i'm wrong
tspan=[0:1800];
x0=0;
[t,x]=ode45(@(t,x)EQ(tspan,x0,a,k,m,F,omega);
plot(t,x);
function sol= EQ(t,x)
a=1;
k=20;
m=0.5;
F=0.01;
omega=2*pi;
x=[(F/k)*cos(omega*t)- (m/k)*d2xdt2- (a/k)*(dxdt)^2]
sol=x ;
end

 Akzeptierte Antwort

James Tursa
James Tursa am 19 Feb. 2021

0 Stimmen

Starting with this differential equation:
m*d2xdt2 + a*(dxdt)^2 + k*x= F*cos(omega*t)
The first step is to solve the equation for the highest order derivative appearing in the equation. This results in:
d2xdt2 = (F*cos(omega*t) - a*(dxdt)^2 - k*x)/m
Now rewrite this as two first order equations using a 2-element vector y, where y(1) is defined to be x and y(2) is defined to be dxdt:
dy(1)dt = dxdt = y(2)
dy(2)dt = d(dxdt)dt = d2xdt2 = (F*cos(omega*t) - a*(dxdt)^2 - k*x)/m = (F*cos(omega*t) - a*(y(2))^2 - k*y(1))/m
From that you can define a derivative function. E.g., expressed in a function handle:
a = 1;
k = 20;
m = 0.5;
F = 0.01;
omega = 2*pi;
dydt = @(t,y) [y(2);(F*cos(omega*t) - a*y(2)^2 - k*y(1))/m];
This function handle is what you can pass to ode45( ).

13 Kommentare

Jasneet Singh
Jasneet Singh am 19 Feb. 2021
Then in this case do i need to include y(1)=dx ; y(2)=dx/dt ; As seperate statements? To define y(1) and y(2)?
Walter Roberson
Walter Roberson am 19 Feb. 2021
Bearbeitet: Walter Roberson am 19 Feb. 2021
No. y is defined as whatever ode45 passes in as the second parameter.
Jasneet Singh
Jasneet Singh am 19 Feb. 2021
So matlab automatically understands whenever I write y(1) as 2nd parameter to ode and later on y(2) as differential of x ..is that a function/command for this operation? Can I use it in every ode solving scenarios like for ode 15s ,etc
Jasneet Singh
Jasneet Singh am 19 Feb. 2021
Walter is this the only error In my script after this would I be able to get values for my ode?
James Tursa
James Tursa am 19 Feb. 2021
Bearbeitet: James Tursa am 19 Feb. 2021
Just use your initial conditions and tspan with this. But your initial conditions need to include both x and dxdt, not just x as you have shown. So something like
tspan = [0:1800];
y0 = [0;0]; % both x and dxdt initial conditions
[t,y]=ode45(dydt,tspan,y0);
plot(t,y(:,1));
Jasneet Singh
Jasneet Singh am 20 Feb. 2021
Yes I have tried this but it's still not working
Previously I was able to find the value sby typing [t,x] in command window and do the plotting but now it only says y is undefined!!!
James Tursa
James Tursa am 20 Feb. 2021
Please show your current code.
tspan=[0:1800];
x0=0;
[t,x]=ode45(@(t,x)EQ(tspan,x0,a,k,m,F,omega);
function sol= EQ(t,x)
a=1;
k=20;
m=0.5;
F=0.01;
omega=2*pi;
dx2dt2=[(F*cos(omega*t))/m - (k/m)*x - (a/m)*(dxdt)^2];
sol=dx2dt2;
end
this works in term of provinding values and plotting graph for [t,x] when i type output:[t,x] in command window but i guess still there is some problem with it ...
if possible please build a code with comments to understand syntax for statements..over this function
tspan=[0:1800];
x0=0;
[t,x]=ode45(@(t,x)EQ(tspan,x0,a,k,m,F,omega);
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
function sol= EQ(t,x)
a=1;
k=20;
m=0.5;
F=0.01;
omega=2*pi;
dx2dt2=[(F*cos(omega*t))/m - (k/m)*x - (a/m)*(dxdt)^2];
sol=dx2dt2;
end
The syntax of ode45() is:
[t,x] = ode45(FUNCTION_HANDLE, TSPAN, INITIAL_CONDITIONS);
For example,
OBJ = @(t,y) [y(2); y(2).^2 - y(1)];
[t,x] = ode45(OBJ, [0 10], [.1, -.03])
t = 57×1
0 0.0152 0.0304 0.0456 0.0608 0.1369 0.2129 0.2890 0.3650 0.5638
x = 57×2
0.1000 -0.0300 0.0995 -0.0315 0.0990 -0.0330 0.0985 -0.0345 0.0980 -0.0360 0.0950 -0.0432 0.0914 -0.0501 0.0874 -0.0567 0.0828 -0.0629 0.0689 -0.0770
plot(t, x)
James Tursa
James Tursa am 20 Feb. 2021
@Jasneet Singh You seem to have ignored all of the code I suggested. Why not give it a try?
Jasneet Singh
Jasneet Singh am 20 Feb. 2021
Ok

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 19 Feb. 2021

0 Stimmen

[t,x]=ode45(@(t,x)EQ(tspan,x0,a,k,m,F,omega);
That line tells us that ode45 is to call an anonymous function passing in two parameters, and that it is to throw away the parameters and call EQ passing in 7 variables whose values had to exist at the time the @ function handle was constructed
But ode45 must be passed at least 3 parameters not just one. The second passed to ode45 must be the time span and the third must be the initial conditions. So ode45 would fail.
If you were to pass the time span and initial conditions to ode45 then it would try to call the anonymous function, which would require recalling those 7 variables. However only tspan and x0 exist, and it would fail trying to find the other ones.
function sol= EQ(t,x)
If the variables did all exist then matlab would try to call EQ but would fail because EQ only expects two parameters.

1 Kommentar

Jasneet Singh
Jasneet Singh am 19 Feb. 2021
I got your concern, I do have values for all parameters except x .. Then is it correct? I'm facing issue while combining scripts for function and ode when I try to run it. And also if I place any other variable in place of x and bring some changes to script, it says undefined variable x , t etc, Even though they are my outputs.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Hilfe-Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by