MATLAB: How to solve y”+y’-2y=x

differential equationsodeode45

Hi all,
I want to solve the equation y''+y'=x I am new to matlab so I don't really know how to do that. I know that I have to use ode etc. but I keep getting various errors so I haven't understood the basic concept yet. I hope that some of you guys can help me out?
Best

Best Answer

Something like this perhaps:
Y0 = [2 5]; % intial conditions
xspan = [0 5]; % x-boundary values
% ode solver
[x, Y] = ode45(@rates, xspan, Y0);
% note that Y(:,1) are the y values and Y(:,2) are the y' values
% Plot results
plot(x, Y)
function dYdx = rates(x,Y)
y = Y(1);
v = Y(2);
dydx = v;
dvdx = -v + 2*y +x.^2 + x;
dYdx = [dydx; dvdx];
end