MATLAB lesson 0: An introduction to MATLAB

1/31/03

Joe Kolesar

Fred Smith

Jenn Howard

 

1. Plot y = x2 and y = 2x on the same set of axes. Where do they intersect?

syms x  

ezplot(x^2), hold on, ezplot(2^x), hold off 

 

  

This graph shows three intersection points, we increase the domain to check for other places of intersection.

 

ezplot(x^2,[-10,10]), hold on, ezplot(2^x,[-10,10]), hold off  

 

  

Here we see that the exponential (2^x) is increasing much faster than the parabola, son it's safe to assume there are no points of intersection to the right of x=10. Also to the left of x=-10 the exponential is close to zero while the parabola is greater than 100, so it's unlikely there are intersection points to the left of x=-10.

 

 

 

We can see algebraically that the two graphs intersect at x=2 since (2)^2=2^(2).  To find the other points we use matlab (solve command) and only get an approximation to the answer.

 

solve('2^x=x^2')  

 

ans =

[                              2]

[                              4]

[ -2*lambertw(1/2*log(2))/log(2)]  

 

here we see x=2 and x=4 which we could have guess by looking at the graph.  the third line with lambertw isn't helpful in finding the leftmost x-value.  We find this using a newtons method program.  looking on page 17 of "A guide to matlab" we find that the command name for numerically solving an equation is 'fzero'.  Note the initial gues of –2 was from the graph

fzero(inline('x^2-2^x'),-2)  

 

ans =

   -0.7667  

To more decimal places we have

vpa(ans,20)  

 

ans =

-.76666469596212316606  

2. Turn on format long, set a=1, and then repeat the command a=(a+2/a)/2 about six times. (For the moment it's easiest to do this from the Command Window with the "uparrow" key. Later we will see how to do this with a loop.) What do you observe, and why? Compare with sqrt(2).

format long  

a=1  

 

a =

     1  

a=(a+2/a)/2  

 

a =

   1.50000000000000  

Repeating this process, we get:

 

a=(a+2/a)/2 

 

a =

   1.41666666666667  

a=(a+2/a)/2 

 

a =

   1.41421568627451  

a=(a+2/a)/2 

 

a =

   1.41421356237469  

a=(a+2/a)/2 

 

a =

   1.41421356237309  

a=(a+2/a)/2 

 

a =

   1.41421356237309  

 

The values of a seem to approach sqrt(2)  

 

ans =

   1.41421356237310  

 

 

this is because if we solve the equation a=(a+2/a)/2, we get 2a=(a^2+2)/a, so 2a^2=a^2+2, so a^2=2, or using matlab,

solve('a=(a+2/a)/2')  

 

ans =

[  2^(1/2)]

[ -2^(1/2)]  

 

3. Plot cosh(x) and sinh(x) on the same set of axes, with x running from ? to 5. What do you observe? For an explanation, try syms x; y = simple(cosh(x)-sinh(x)).

syms x, ezplot(cosh(x)),hold on, ezplot(sinh(x),[-5,5]),hold off  

 

  

 

y = simple(cosh(x)-sinh(x))  

 

y =

1/exp(x)  

 

This means as x approaches infinity, sinh(x) approaches cosh(x) very quickly (with exponential convergence) whereas when x apptoaches –infinity, sinh(x) and cosh(x) diverge from each other exponentially.

4. Compare sin(pi/6) and cos(pi/6) with sin(sym('pi/6')) and cos(sym('pi/6')).

sin(pi/6)  

 

ans =

   0.50000000000000  

cos(pi/6)  

 

ans =

   0.86602540378444  

this is an approximation to the irrational number

 

sin(sym('pi/6'))  

 

ans =

1/2  

cos(sym('pi/6'))  

 

ans =

1/2*3^(1/2)  

here is the exact irrational number.