I have used two different methods for same problem and getting different plots

%----------------CODE - 1-----------------------------
function second_order_ode
clc
clear
t = 0:0.001:3; % time scale
initial_x = 0;
initial_dxdt = 0;
[t,x] = ode45 ( @rhs, t, [initial_x, initial_dxdt]);
plot(t,x(:,2));
xlabel('t'); ylabel('x');
title('Solution to ODE d^2x/dt^2+5dx/dt-4x(t)=sin(10t)')
disp([t,x(:,2)])
function dxdt=rhs(t,x)
dxdt_1 = x(2);
dxdt_2 = -5*x(2)+4*x(1)+sin(10*t);
dxdt = [dxdt_1; dxdt_2];
end
end
%----------------CODE - 2-----------------------------
clc
clear
syms x t
x = Dsolve('D2x + 5*Dx - 4*x = sin(10*t)','x(0)=0','Dx(0)=0','t')
tt = 0:.01:3
xx = subs(x,t,tt)
plot(tt,xx)

6 件のコメント

MathReallyWorks
MathReallyWorks 2017 年 5 月 29 日
Your code is not running properly. Please edit it.
Attach your output as well to get the proper answer.
%----------------CODE - 1-----------------------------
function second_order_ode
clc
clear
t = 0:0.001:3; % time scale
initial_x = 0;
initial_dxdt = 0;
[t,x] = ode45 ( @rhs, t, [initial_x, initial_dxdt]);
plot(t,x(:,2));
xlabel('t'); ylabel('x');
title('Solution to ODE d^2x/dt^2+5dx/dt-4x(t)=sin(10t)')
disp([t,x(:,2)])
function dxdt=rhs(t,x)
dxdt_1 = x(2);
dxdt_2 = -5*x(2)+4*x(1)+sin(10*t);
dxdt = [dxdt_1; dxdt_2];
end
end
if true
% code
end
%----------------CODE - 2-----------------------------
clc
clear
syms x t
x = Dsolve('D2x + 5*Dx - 4*x = sin(10*t)','x(0)=0','Dx(0)=0','t')
tt = 0:.01:3
xx = subs(x,t,tt)
plot(tt,xx)
Taha Abbas Bin Rashid
Taha Abbas Bin Rashid 2017 年 5 月 29 日
Try running both codes separately
Taha Abbas Bin Rashid
Taha Abbas Bin Rashid 2017 年 5 月 29 日
Output of Code 1
Taha Abbas Bin Rashid
Taha Abbas Bin Rashid 2017 年 5 月 29 日
Output of code 2

サインインしてコメントする。

 採用された回答

Walter Roberson
Walter Roberson 2017 年 5 月 29 日

0 投票

After you find the numeric solution, you plot the second output, which is the derivative.
After you find the symbolic solution, you plot the function itself.

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by