Solving second order differential equation with initial conditions
Show older comments
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
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
on 3 Mar 2016
Star Strider
on 3 Mar 2016
First, the syntax you used in your dsolve call is obsolete in R2015b (the same version I’m using).
Second, I did in my Answer to your previous Question. I’m re-posting it here:
syms y(t) p w t
p = 0;
Deq = diff(y,2) + p*diff(y) + 2*y == cos(w*t);
Y = dsolve(Deq, diff(y(0)) == 0, y(0) == 0);
Y = simplify(Y, 'steps', 20);
Yfcn = matlabFunction(Y)
It should run without error in R2015b.
Francisco Zapata
on 3 Mar 2016
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')
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!