## Matlab Help

Matlab is availabile with limited online access from the MyMath website; after login, use "Goto" to follow the "Software" link and select Matlab. More information about Matlab is given at the Matlab information website, and the MyMath website.

Here is a Matlab example command for plotting a simple function x^3-4x^2+4x-1, for x in [-1,3]:
ezplot( 'x^3 - 4*x^2 + 4*x - 1', [-1 3] )

Here are some Matlab example commands for plotting two functions on the same graph:
ezplot( 'x^3 - 4*x^2 + 4*x - 1', [-1 3] ), hold on
ezplot( 'x^4 - 2*x^3 + cos(2*x)', [-1 3] )

Here are some Matlab example commands for finding a best-fit line and plotting it with the data:
x = [5.8 1.5 2.3 1 3.3]; y = [8.6 1.9 3.1 1 5];
p = polyfit( x, y, 1 ); m = p(1), b = p(2)
plot( x, y, '*', x, b + m*x )
title('Some Bird Eggs Data and Best-Fit Line')
xlabel('Egg Width'); ylabel('Egg Length')

Some Matlab example commands for computing a table of values for the function f(x) = (x^4+x-18)/(x^2-4) near x=2:
x = [1.9995 : .0002 : 2.0005];
f = (x.^4 + x - 18)./(x.^2 - 4);
disp([x' f'])

Some Matlab example commands for finding an approximate root of f(x) = 1+sin(x)+x^3, for x in [-1,1]:
f = @(x) 1 + sin(x) + x^3; a = -1; b = 1; c = (a+b)/2;
for i=1:8, if f(a)*f(c)<0, b=c;else,a=c;end, c=(a+b)/2; disp([a b c f(c)]); end

Some Matlab example commands for computing a sequence an+1=1/(5-an), with a1 = 1:
a = 1; disp([ 1 a ]), for i = 2:10, a = a/(5-a); disp([ i a ]), end

Some Matlab example commands for computing an approximate derivative for x^ln(x) at x = 2:
f = @(x)x^log(x); a = 2;
for i = 1:10, h = 1/2^i; disp([h (f(a+h)-f(a))/h]), end

Here is a Matlab command for plotting the implicitly defined function y(x) defined by x^2 + y^2 - 1 )^3 = x^2y^3 :
ezplot( '( x^2 + y^2 - 1 )^3 - x^2*y^3', [-1.5,1.5] )

Some Matlab example commands for (approximately) finding a local maximum of f(x)=x^3-4x^2+4x-1 and the point where the maximum occurs:
f = @(x) x.^3 - 4*x.^2 + 4*x - 1; x = [-1:.01:2];
[ M, j] = max(f(x)); disp([M x(j)])

Some Matlab example commands for finding an approximation to the definite integral of f(x) for x from 1 to 6 using the midpoint rule with n = 32 points:
f = @(x)x.^3 - 4*x.^2 + 4*x - 1;
n = 32; a = 1; b = 6; Delta = (b-a)/n;
x = a + Delta*[1:2:2*n-1]/2; M = Delta*sum(f(x));
disp(M)

Some Matlab example commands for finding an approximation to the definite integral of f(x) for x from 1 to 6 using the trapezoidal rule and Simpson's rule with n = 32 points:
f = @(x)x.^3 - 4*x.^2 + 4*x - 1;
n = 32; a = 1; b = 6; Delta = (b-a)/n;
T = Delta*( f(a)/2 + sum(f(a+[1:n-1]*Delta)) + f(b)/2 );
disp(T) % Trapezoidal Rule
O = f( a + [1:2:n-1]*Delta ); E = f( a + [2:2:n-2]*Delta );
S = Delta*( f(a) + 4*sum(O) + 2*sum(E) + f(b) )/3
disp(S) % Simpson's Rule

Here is a Matlab command for plotting the surface z = x^2+2y^2+4:
ezsurf('x^2+2*y^2+4')
Note: Matlab does not orient the axes in the standard way, but you can do this with the "view" command. For a more standard view, try
ezsurf('x^2+2*y^2+4'); view([135 15])

Here are some Matlab commands for plotting the surface z = x^2+2y^2+4 and the tangent plane to the surface at (x,y,z)=(1,-1,7):
ezsurf('x^2+2*y^2+4'); hold on
ezsurf('7+2*(x-1)-4*(y+1)');