Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

function second_order_ode_4

clc
clear
format long g
tic
t= -0:0.001:7;
initial_x = 5;
initial_dxdt = 3;
[t,x] = ode45( @rhs, t, [initial_x, initial_dxdt])
plot(t,x(:,1));
xlabel('t'); ylabel('x');
title("Solution of ODE")
disp([t,x(:,2)])
y = toc;
display(['Computational time required for Numerical Solution is: ', num2str(y)])

%--------------------------------OUTPUT DATA FILE--------------------------


Matrix=[t x(:,1)];
Numerical_Values = fopen('Numerical_Values.txt','wt');
for row = 1:length(Matrix)
Row = Matrix(row,:);
fprintf(Numerical_Values,'%d %d\n',Row);
end

end

function dxdt=rhs(t,x)
dxdt_1 = x(2);
dxdt_2 = -7*x(2)-10*x(1)+20;
dxdt = [dxdt_1; dxdt_2];
end

You might also like