# How to solve this diff equation in matlab?

The ODE

\begin{cases} y'' − Cxy = g(x),\\ y(2) = 1,\\ y'(2) = 0, \end{cases}

where

$$g(x) = \begin{cases} −1 & 2 \leq x \leq 3, \\ −1/3 & 3 \leq x \leq 5, \end{cases}$$

should get solved for for $C=0.8$, $1$, and $2$ at the interval $2 \leq x \leq 5$.

I must write a MATLAB program that performs the calculation and draws the $3$ solution curves in the same graph.

I should rewrite the problem as a system of first order:

$u_1 = y,\\ u_2 = y',\\ u_2' = y''.$

Hence

$u_2'-Cxu_1=g(x),\\ u_1(2)=1. u_2(0)=2.$

How do I continue?

## Update

I used this function file in matlab

function f=func(x,u)
global C;
if x<3
g=-1;
else
g=-x/3;
end
f=[u(2)
C*x*u(1)+g];


then I run this program

>> global C;
>> for C=[0.05 0.1 0.2]
[X, U]=ode45(@func,[2 5],[1;0]);
plot(X,U(:,1)); hold on
end


and I get this graph, is it correct?

-
Have you written any code yet? Especially since this is a homework question, it is good to demonstrate that you've tried something before you ask for help. – Richie Cotton Sep 21 '12 at 9:44
@RichieCotton thank you for the comment. I've now made an extensive effort and nearly solved the entire problem if you want to have a look and comment my code that I tried to write. I'm not sure whether my solution is correct. – Nick Rosencrantz Sep 21 '12 at 12:15