How to use initial conditions with an equation?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Luis
am 18 Jun. 2014
Beantwortet: Star Strider
am 18 Jun. 2014
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?
0 Kommentare
Akzeptierte Antwort
Star Strider
am 18 Jun. 2014
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')
0 Kommentare
Weitere Antworten (1)
Geoff Hayes
am 18 Jun. 2014
Luis - no you don't need to use a for loop, but you will have to adjust your equations so that they can compute elementwise operations (i.e. handle vector input rather than just a scalar input).
To do this, any operation that affects your vector must be preceded by a period. Your first equation
Y=2*x^3+6*x^2-10*x+4
becomes
Y=2*x.^3+6*x.^2-10*x+4
Note that it was only necessary to precede the exponent operator with a period; all other operations are handled normally.
Then with
x=0:0.1:6
you can calculate Y with the above equation. You can do the same sort of thing for the derivative.
Note that you mention that your initial conditions are x=0 and y=1. Why is y=1, since if x=0 then your equation evaluates to 4?
Try the above and see what happens!
0 Kommentare
Siehe auch
Kategorien
Mehr zu Function Creation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!