# Math 151 Lab 03 - 01/24/2018:

DUE: Monday, January 29, 2018 (by 4:00pm before class starts)

INSTRUCTIONS: Save this Matlab script with the filename LastName_Lab03.m (example: Lewis_Lab03.m). Complete each question (either in words or with Matlab code). When you are ready to have your assignment graded, choose the PUBLISH command from the file menu, and submit the resulting file to CANVAS.

Please make sure to leave code uncommented unless I ask for a comment. You do not need to label your steps for each problem, but I should be able to see you do each step I asked for.

## #1

WRITE CODE BELOW:

Generate two separate plots: y=exp(-x) on the interval [0,1], and y=1/x on the interval [1,2]. Have the first function display as a blue line and the second display as a red line.

------------------------------------------------------------------------

```figure
x = linspace(0,1);
y = exp(-x);
plot(x,y,'b')
figure
x = linspace(1,2);
y = 1./x;
plot(x,y,'r')
```

## #2

WRITE CODE BELOW:

Step 1: Plot the function x*e^x-cos(x) on the interval [-2,2]

Step 2: Plot the horizontal line y=0 in the same window.

Step 3: Lastly, in the same window, plot the two points where x*e^x-cos(x) crosses the x-axis with the color red (these should display as individual markers that are visible on the plot).

------------------------------------------------------------------------

```x = linspace(-2,2);
y = x.*exp(x)-cos(x);
y0 = 0*x;
figure
plot(x,y,'b',x,y0,'g',-1.864,0,'ro',0.518,0,'ro')
%could also use the hold on command and multiple plot commands
```

## #3

WRITE CODE BELOW:

Read the Number3.pdf file about Newton's method. We're going to find the zeroes of that function.

Step 1: Initialize a vector x of 50 entries. Set the first entry equal to your initial value (i.e. x(1)=1) (Note that normally we would just update the variable x, but I want you to see the convergence.)

Step 2: Fill the remaining entries of x in a loop using the last formula in m151q3.pdf

Step 3: Repeat parts 1 and 2 until you have found approximations for all 5 zeros of the function. Output these approximations to the screen individually.

------------------------------------------------------------------------

```x=zeros(1,50);
x(1) = 1;
for i = [2:50]
x(i) = x(i-1) - (x(i-1)^5-2*x(i-1)^4+x(i-1)^2-0.05)/(5*x(i-1)^4-8*x(i-1)^3+2*x(i-1));
end
x(50)

x=zeros(1,50);
x(1) = -0.5;
for i = [2:50]
x(i) = x(i-1) - (x(i-1)^5-2*x(i-1)^4+x(i-1)^2-0.05)/(5*x(i-1)^4-8*x(i-1)^3+2*x(i-1));
end
x(50)

x=zeros(1,50);
x(1) = -0.2;
for i = [2:50]
x(i) = x(i-1) - (x(i-1)^5-2*x(i-1)^4+x(i-1)^2-0.05)/(5*x(i-1)^4-8*x(i-1)^3+2*x(i-1));
end
x(50)

x=zeros(1,50);
x(1) = 0.2;
for i = [2:50]
x(i) = x(i-1) - (x(i-1)^5-2*x(i-1)^4+x(i-1)^2-0.05)/(5*x(i-1)^4-8*x(i-1)^3+2*x(i-1));
end
x(50)

x=zeros(1,50);
x(1) = 1.6;
for i = [2:50]
x(i) = x(i-1) - (x(i-1)^5-2*x(i-1)^4+x(i-1)^2-0.05)/(5*x(i-1)^4-8*x(i-1)^3+2*x(i-1));
end
x(50)
```
```ans =

0.9469

ans =

-0.5741

ans =

-0.2395

ans =

0.2354

ans =

1.6313

```

## #4

WRITE CODE BELOW:

Make A into a random 3x5 matrix (using the matlab command for a random matrix). Try plotting the matrix A.

WRITE A COMMENT BELOW:

What does plot(A) do? Please explain in detail how the axes are shown and what the picture means so I know you understand.

------------------------------------------------------------------------

The columns are plotted as 5 lines of 3 data points. The x axis is taken to be the integers 1,2,3 for each row entry in the column.

```A = rand(3,5);
plot(A)
```

## #5

WRITE CODE BELOW:

With -pi<x<pi and -pi<y<pi, plot the 3D function Z=sin(X)+cos(Y)-0.3*sin(4*X)+0.1*cos(5*Y)

------------------------------------------------------------------------

```x = linspace(-pi,pi);
y = linspace(-pi,pi);
[X,Y] = meshgrid(x,y);
Z = sin(X)+cos(Y)-0.3*sin(4*X)+0.1*cos(5*Y);
surf(X,Y,Z)
```