MATLAB: Different Approach to solve ODE

new approachode

Hello all,
I am trying to solve an ODE but I am not sure if the approach using ode45 is right.
1st Approach:
% Quadratic damping Response
tspan = 0:1800;
y0 = [0;0];
[t,y] = ode45 (@forced,tspan,y0);
plot(t,y(:,1));
grid on
xlabel ('time(s)')
ylabel ('Displacement(m)')
title ('System Response')
hold on
% Linear Damping Response
f = 0.01; % Force (N)
c = 0.01; % Damping coefficient (N.s/m)
k = 20;
w = 2*pi; % frequency (rad/s)
m = 0.5; % Mass (Kg)
wn = sqrt(k/m); % Natural Frequency
cc = 2*m*wn; % Critical Damping
z = c/cc;
a = -(2*z*w*wn*f/m)/((wn^2 - w^2)^2 + (2*z*w*wn)^2);
b = ((wn^2 - w^2)*f/m)/((wn^2 - w^2)^2 + (2*z*w*wn)^2);
X = sqrt(a^2 + b^2);
fi = -atan(a/b);
x = X*sin(6.28*t - fi);
plot(t,x);
function yp = forced(t,y)
yp = [y(2);
0.01/0.5*cos(2*pi*t)-1/0.5*y(2)^2-20/0.5*y(1)];
end
2nd Approach
syms x(t)
Dx = diff(x);
ode = diff(x,t,2) == 0.01*cos(2*pi*t) - 2*(diff(x,t,1))^2 - 40*x;
xSol(t)= dsolve(ode);
This second approach gives me the warning that it is unable to find symbolic solution. I already have installed the symbolic toolbox but, still getting the same results.
Any help?
Thank you in Advance

Best Answer

I have no idea how the ‘Linear Damping Response’ section figures into this.
However:
syms t x(t) Y
Dx = diff(x);
ode = diff(x,t,2) == 0.01*cos(2*pi*t) - 2*(diff(x,t,1))^2 - 40*x;
[VF,Sbs] = odeToVectorField(ode);
forced = matlabFunction(VF, 'Vars',{t,Y});
% Quadratic damping Response
tspan = 0:1800;
y0 = [0;0];
[t,y] = ode45 (forced,tspan,y0);
figure
plot(t,y(:,1));
grid on
xlabel ('time(s)')
ylabel ('Displacement(m)')
title ('System Response')
hold on
will do what you want with respect to integrating the symbolic expression.
The matlabFunction function creates an anonymous function version of the ODE from the vector field created by odeToVectorField. See the documentation section on Anonymous Functions for details on them and how to use them.