How to solve and plot second order differential equation using ode45?

Hey there
Im trying to solve and plot the following differential equation using ode45
x''=(-2k*x-2c*x'-r*omega*(cos(omega*t)+(r/L)*cos(2*omega*t))*m+x0*M)/M+m
where
M = 22
m = 0.9
k = 25000
c = 2
omega = 860
L = 0.2
r = 0.07
The starting conditions are :
x0=0
x'0=0
I've tried a bunch of different tutorials, but keep getting different error messages.
Any help is greatly appreciated, thank you!

댓글 수: 4

Have you looked into the documentation of ode45 to see how to formulate the ODE properly?
"I've tried a bunch of different tutorials, but keep getting different error messages."
Can you share the code you tried and the corresponding errors you got?
Ye, but im not sure im understanding it correctly
Here is one of my attempts:
x0 = [0 0];
tspan = 0:.1:10;
ode45(@dxdt,tspan,x0)
plot(t,x)
function dotx = dxdt(t,x)
M = 22;
m = 0.9;
k = 25000;
c = 2;
omega = 860;
l = 0.2;
r = 0.07;
x0_speed = 0;
x0_pos = 0;
dx1=x(2);
dx2=(-2*k*x(2)-2*c*dx1-r*omega^2*(cos(omega*t)+(r/l)*cos(2*omega*t))*m+x0_pos*(M-m))/M;
end
And i got this error message:
Output argument "dotx" (and possibly others)
not assigned a value in the execution with
"Assignment2_a_forsog_2>dxdt" function.
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 106)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in Assignment2_a_forsog_2 (line 4)
ode45(@dxdt,tspan,x0)
Is the value of x0 that appears in the ODE the same as the value of x0 that is one of the initial conditions?
Also, you have defined the variable x_speed in the ODE function, but have not used it. Is there any particular use of that variable?
Ye, x0 is the same, its just part the equation i arrived at during calculations.
No x_speed isnt used anywhere.

댓글을 달려면 로그인하십시오.

답변 (1개)

Fabio Freschi
Fabio Freschi 2023년 11월 23일
편집: Fabio Freschi 2023년 11월 23일
In your code there is a mistake in the definition of x(1) and x(2)
I have made a few stylistic changes (parameters outside the function, use of implicit function) and the correction of the equations. In addition, I suggest to let ode45 to choose the timestep and keep the t vector provided as output for the plot
% clear variables, close all
x0 = [0 0];
tspan = [0 10];
% params
M = 22;
m = 0.9;
k = 25000;
c = 2;
omega = 860;
l = 0.2;
r = 0.07;
x0_speed = 0;
x0_pos = 0;
% implicit function
% changes here
% | |
% V V
dxdt = @(t,x)[x(2); (-2*k*x(1)-2*c*x(2)-r*omega^2*(cos(omega*t)+(r/l)*cos(2*omega*t))*m+x0_pos*(M-m))/M];
[t,x] = ode45(dxdt,tspan,x0);
figure
plot(t,x)
figure
plot(t,x)
xlim([0 0.5])

댓글 수: 4

Thank you so much!
Don't forget to accept Prof. @Fabio Freschi's helpful answer.
I double-check. The original 2nd-order differential equation in your question
is slightly different from the state equation you defined in your code:
( - 2*k*x(1) - 2*c*x(2) - r*omega^2*( cos(omega*t) + (r/l)*cos(2*omega*t) )*m + x0_pos*(M - m) )/M;
Please clarify!
This is why I wrote my code according to the orignal equation (as stated in the comment)

댓글을 달려면 로그인하십시오.

카테고리

제품

릴리스

R2021b

질문:

2023년 11월 23일

댓글:

2023년 11월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by