% Last update: 03/05/04 %----------------------------------------------------- %MATLAB COMMAND EXPLANATION (by Luis Carvalho) %----------------------------------------------------- % general >> clear % clear variables (if any) >> f = inline('(3*x - y)/2') % define a function f(x) >> f(0,1) % evaluate f at x=0 and y=1 (f(0,1)=-0.5) >> pi % the number 3.1415... . >> exp(2.5) % exponential function evaluated at 2.5 >> ln(2.5) % the natural logarithm of 2.5 >> x = 1 + 2 + 3 + 4 + 5 + ... % if the expression is long 6 + 7 + 8 + 9 + 10 % you can break the lines using "..." >> p = 'x^2 + 2*x' % store the expression x^2+2*x (as a string) % under the name "p" % plotting functions and points >> p = inline('x.^2 + 2.*x') % we can now use p as a function >> x = 0:0.1:1 % x = [0, 0.1, 0.2, ..., 0.9, 1], a vector >> x(2) % second position in x, x(2) = 0.1 >> y = p(x) % y = [p(0), p(0.1), p(0.2), ..., p(1)] % y is a vector with the same size as x >> length(y) % the size (# of entries) of y: 11 >> plot(x, y, 'b-') % plot x against y, using a blue (b) line (-) >> y2 = y % y2 is a copy of y >> y2(3) = 1 % change the third entry of y2 to 1 >> plot(x, y2, 'r*') % plot x against y2 using red (r) starts (*) >> plot(x, y, 'b-', x, y2, 'r*')% plot x against y and y2 >> hold on % you can also use "hold on" to pile up plots >> plot(x, y, 'ko') % plot y now in black (k) circles (o) >> plot(x, y2, 'g.') % and y2 in green (g) dots (.) >> hold off % quit holding % solving equations >> solve('cos(x) + y = 9', 'x') % solve an equation for x >> [x, y] = solve('x^2 * y^2 = 0', ... 'x - y = 1') % solve a system of equations % and store results in x and y >> fsolve('x.^3 - 2.*x + 1, [-1, 1]) % numerically solve x^3 - 2*x + 1 = 0 % for x between -1 and 1 %% differential equations >> dsolve('Dx = 3*x*(1-x/300)') % solve the ODE dx/dt = 3*x*(1-x/300). Note that % "D" denotes differentiation with respect to "t" >> diffeq = 'Dx = 3*x*(1-x/300)'% use variables to get shorter commands >> dsolve(diffeq, 'x(0) = 50') % now we have an IVP >> y = dsolve('D2y = sin(y)') % you can also specify second order ODEs... >> x = dsolve('D3x = -x', ...'x(0)=1, Dx(0)=0, D2x(0)=0') % ... higher order IVPs >> ezplot(x) % and plot them