Solving second order differential equation with initial conditions

14 views (last 30 days)
Hi, I am completely new to Matlab and having trouble solving and plotting this second order DE.
I am asked to solve the equation: D^2y/dt + p(dy/dt) + 2y = cos(w*t) with the conditions y(0) = 0 and y'(0) = 0
Assuming p = 0 and w cannot be 0.
This is my attempt at a solution:
syms y(t) p w t
p = 0;
y = dsolve('D2y + p*Dy + 2*y = cos(w*t)','y(0) = 0','Dy(0) = 0','t');
When I enter this into Matlab, I get no answer. Any help on what I could do?
I am also asked to plot solution of the y(t) vs t for other values of w such as 0.5,0.6, etc. How would I go about doing this? Any help or solutions would be greatly appreciated!

Answers (1)

Star Strider
Star Strider on 3 Mar 2016
This may be obsolete syntax:
y = dsolve('D2y + p*Dy + 2*y = cos(w*t)','y(0) = 0','Dy(0) = 0','t');
since I do not know what version of MATLAB you are using.
The code I posted in your previous Question Solve Second Order Differential Equation with Initial Conditions gives you the symbolic answer, and an anonymous function (that I put on one line here):
Yfcn = @(C,t,w) C.*sin(sqrt(2.0).*t)+(cos(sqrt(2.0).*t)-cos(t.*w))./(w.^2-2.0);
You have to supply the ‘C’ constant, but otherwise you can simply use the output from meshgrid to create your ‘t’ and ‘w’ matrices and supply them as arguments to the ‘Yfcn’ function. Then use surf or mesh with the matrix produced by a call to ‘Yfcn’ to plot the result.
  4 Comments
Francisco Zapata
Francisco Zapata on 3 Mar 2016
Okay I'm understanding but I don't know how to implement the meshgrid to plot the function with the values of w. Do you have code for that as well? Sorry I'm really new and have not used most of these functions.
Star Strider
Star Strider on 3 Mar 2016
MATLAB can be a bit intimidating at the beginning because it’s possible to do so much with it.
This is what I would do:
Yfcn = @(C,t,w) C.*sin(sqrt(2.0).*t)+(cos(sqrt(2.0).*t)-cos(t.*w))./(w.^2-2.0);
t = linspace(0, 10, 25);
w = linspace(0, 2*pi, 50);
[T,W] = meshgrid(t,w);
C = 1; % Constant
Y = Yfcn(C,T,W);
figure(1)
surf(T, W, Y)
grid on
xlabel('Time')
ylabel('Angular Frequency (rad)')
zlabel('Y')

Sign in to comment.

Categories

Find more on Symbolic Math Toolbox 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!