MATLAB: How to use initial conditions with an equation

equationgraphinitial conditionsodeplotting

Hello I'm wondering how can I use the given initial conditions to an equation e.g. x=0 and y=1 to be used on an equation e.g. Y=2*x^3+6*x^2-10*x+4 and its derivative dydx=6*x^2+12x-10 when I try to plot Y and dydx from 0 to 6 with 0.1 steps. Do I need a for loop or something of that matter?

Best Answer

The easiest way is to use polyval:
% Y=2*x^3+6*x^2-10*x+4
% dydx=6*x^2+12x-10
Y = [2 6 -10 4]; % EXPRESS COEFFICIENTS AS A VECTOR
dydx = [6 12 -10];
x = 0:0.1:6;
Y_x = polyval(Y,x);
dydx_x = polyval(dydx,x);
figure(1)
plot(x, Y_x, x,dydx_x)
grid
legend('Y', '^{dy}/_{dx}', 'Location', 'NW')