0
votes

I am new to MATLAB and I am trying to figure out to solve a differential equation. My equation is: d^2x/dt^2 - sin(t)*(dx/dt) = x. I am trying to solve for t=10 and assume initial values are specified for t = 0. I have no idea where to start with this any help would be great.

1
You might find this blog post from The MathWorks instructive. - horchler

1 Answers

0
votes

I recommend using state-space modeling syntax, where we treat x as a vector of our state variable (x) and its subsequent derivative.

Here's example code for solving your initial value problem:
(I used FreeMat, but it should be the same for MATLAB)

function [] = ode()

% Time
t_start = 0.0;
t_final = 10.0;

% Lets treat x as a vector
% x(1) = x
% x(2) = dx/dt (first derivative of x)
x0 = [0.1; 0]; % Initial conditions

options = odeset('AbsTol',1e-12,'RelTol',1e-6,'InitialStep',1e-6,'MaxStep',1e-2); % stepping tolerances
[t,x] = ode45(@system, [t_start t_final], x0, options); % Run the differential equation solver

hold on
plot(t,x(:,1),'r-')
plot(t,x(:,2),'b-')
hold off

end

% Define the differential equation
function dxdt =  system(t,x)

dxdt = [x(2); ... % derivative of x(1) is x(2)
x(1) + sin(t)*x(2)]; % derivative of x(2) is x + sin(t)*dx/dt

end

Resulting plot