Solving second order differential equation with initial conditions
조회 수: 11 (최근 30일)
이전 댓글 표시
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!
댓글 수: 0
답변 (1개)
Star Strider
2016년 3월 3일
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
Star Strider
2016년 3월 3일
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')
참고 항목
카테고리
Help Center 및 File Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!