matlab 5 solution.pdf

10
Name: Foster, Antony (XXXX) Course: Math 20300 XX Instructor: Prof A Foster Due Date: June XX, XXXX MATLAB ASSIGNMENT 5 Purpose: Using Matlab to create 2-D labeled/unlabeled contour plots of surfaces defined over closed bounded rectangular regions where we can identify and classify potential critical points (relative extrema) that the function may have on as guaranteed by the Extreme Value Theorem. New Commands Used: plot (x,y, options) text(x,y,'Your Phrase',options} contour(x,y,f(x,y),n,options) [C h] = contour(x,y,f(x,y)) clabel(C,h) u = a:b:c

Upload: muni-sankar-matam

Post on 29-Nov-2015

13 views

Category:

Documents


1 download

DESCRIPTION

solution manual for matlab developers

TRANSCRIPT

Page 1: MATLAB 5 Solution.pdf

Name: Foster, Antony (XXXX)

Course: Math 20300 XX

Instructor: Prof A Foster

Due Date: June XX, XXXX

MATLAB ASSIGNMENT 5

Purpose: Using Matlab to create 2-D labeled/unlabeled contour plots of surfaces

defined over closed bounded rectangular regions where we

can identify and classify potential critical points (relative extrema) that the

function may have on as guaranteed by the Extreme Value Theorem.

New Commands Used: plot (x,y, options)

text(x,y,'Your Phrase',options}

contour(x,y,f(x,y),n,options)

[C h] = contour(x,y,f(x,y))

clabel(C,h)

u = a:b:c

Page 2: MATLAB 5 Solution.pdf

Exercise 5.2 Suppose that a surface S is defined by

A) Write a script M-file that will produce a labeled contour plot for over the (closed and

bounded) region, S, given by

B) Based on the contour plot you found in A) estimate the coordinates of 2 saddle points of

in the region defined in A). Mark these points using the Data Cursor.

MATLAB CODE

clear all clc; clf reset

figure(2)

f = @(x,y)sin(3*y - x.^2 + 1) + cos(2*y.^2 - 2*x);

u = linspace(-2,2,45); v = linspace(-1,1,45); %Levels = [-2:.2:2]; [x,y] = meshgrid(u,v);

[C,h] = contour(x,y,f(x,y),'k'); hold on clabel(C,h);

plot(-1.1818,0.63636,'--rs','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor',

'r','MarkerSize',6)

plot(1,0.77273,'--rs','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor',

'r','MarkerSize',6) plot(0.5,-0.72727,'--rs','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor',

'r','MarkerSize',6)

text(-1.09,0.63636,'\leftarrow This is a saddlepoint','HorizontalAlignment','left',

'FontWeight','bold')

text(1.1,0.77273,'\leftarrow This is a saddle point','HorizontalAlignment','left',

'FontWeight','bold')

text(0.6,-0.72727,'\leftarrow This is a saddle point','HorizontalAlignment','left',

'FontWeight','bold')

set(gca,'XTick',[-2:0.5:2],'XMinorTick','on','FontName','times','FontWeight',

'bold')

Page 3: MATLAB 5 Solution.pdf

set(gca,'YTick',[-1:0.5:1],'YMinorTick','on','FontName','times','FontWeight',

'bold')

title({' ','20 labeled Contour curves of the surface f(x,y) = sin(3y - x^2 + 1) +

cos(2y^2 - 2x)','over the rectangle R = \{(x,y) | -2\,\leq\,x\,\leq 2,\, -

1\,\leq\,y\,\leq\,1\}','by Antony Foster'})

xlabel('X-Axis','Color','red','FontName','mathematica','FontWeight','bold',

'FontSize',12)

ylabel('Y-Axis','Color','red','FontName','mathematica','FontWeight','bold',

'FontSize',12)

axis square axis([-2 2 -1 1]); grid on %%

MATLAB OUTPUT

-1.5

-1.5

-1.5

-1.5

-1.5

-1.5

-1.5

-1.5 -1

.5-1

-1

-1

-1

-1

-1-1

-1

-1

-1

-0.5

-0.5

-0.5

-0.5

-0.5

-0.5

-0.5

-0.5

-0.5

-0.5

0

0

0

0

0

0

0

0

0

0

0

0 00

0.5

0.5 0.5

0.5

0.5

0.5

0.5

0.5

0.5

1

1

1

1

1 1

1

11.5

1.5

1.5

1.5

1.5

This is a saddle point

This is a saddle point

This is a saddle point

10 labeled Contour curves of the surface f(x,y) = sin(3y - x2 + 1) + cos(2y2 - 2x)

over the rectangle S = {(x,y) | -2 x 2, -1 y 1 }

by Antony Foster

X-Axis

Y-A

xis

-2 -1.5 -1 -0.5 0 0.5 1 1.5 2-1

-0.5

0

0.5

1

Page 4: MATLAB 5 Solution.pdf

NOTE: THIS IS NOT THE ANSWER TO PART A) or B). I just wanted to see more

contours curves (40 unlabeled ones) of so that I could better identify the

saddle points (I can see about 4 such points).

MATLAB CODE

clear all clc; clf reset

figure(1)

f = @(x,y)sin(3*y - x.^2 + 1) + cos(2*y.^2 - 2*x);

u = linspace(-2,2,25); v = linspace(-1,1,25); [x,y] = meshgrid(u,v);

n = 40; % number of contour curves. contour(x,y,f(x,y),n,'k'); % 2-Dimensional contour plots of z = f(x,y)

set(gca,'XTick',[-2:0.5:2],'XMinorTick','on','FontName','times','FontWeight',

'bold')

set(gca,'YTick',[-1:0.25:1],'YMinorTick','on','FontName','times','FontWeight',

'bold')

title({' ','40 unlabeled contour curves of f(x,y) = sin(3y - x^2 + 1) + cos(2y^2 -

2x)','over the rectangle R = \{(x,y) | -2\,\leq\,x\,\leq 2,\, -1\,\leq\,y\,\leq\,1

\}','by Antony Foster'})

xlabel('X-Axis','Color','red','FontName','mathematica','FontWeight','bold',

'FontSize',12)

ylabel('Y-Axis','Color','red','FontName','mathematica','FontWeight','bold',

'FontSize',12)

axis equal square axis([-2 2 -1 1]); grid on %%

Page 5: MATLAB 5 Solution.pdf

MATLAB OUTPUT

40 unlabeled contour curves of f(x,y) = sin(3y - x2 + 1) + cos(2y2 - 2x)

over the rectangle R = {(x,y) | -2 x 2, -1 y 1 }

by Antony Foster

X-Axis

Y-A

xis

-2 -1.5 -1 -0.5 0 0.5 1 1.5 2-1

-0.75

-0.5

-0.25

0

0.25

0.5

0.75

1

Page 6: MATLAB 5 Solution.pdf

Exercise 5.3 Consider the surface given by .

A) Write a script M-file that will produce labeled contour curves for over the (closed and bounded)

square region given by

.

B) Based on the contour plot you found in A) determine whether has any critical points in

the square defined in A). If there are any such points, provide estimates from the graph for their

and coordinates and provide a justification from the graph as to whether these points occur at a

relative maxima, minima or saddle points. Indicate your reasons as comments in the script M-file

and publish the contour plot and the script M-file.

MATLAB CODE clear all clc; clf reset figure(4)

f = @(x,y)sin(3*x + y) - 2*cos(x - y);

u = linspace(-2,2,45); v = linspace(-2,2,45);

Levels = -3:0.4:3; [x,y] = meshgrid(u,v);

plot(-1.1818,-1.1818,'--rs','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor',

'r','MarkerSize',10)

hold on plot(-0.36364,-0.36364,'--

rs','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',10)

hold on plot(1.1818,-2,'--

rs','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','b','MarkerSize',10)

hold on [C,h] = contour(x,y,f(x,y),Levels,'k');

clabel(C,h,Levels);

plot(0.36364,0.36364,'--rs','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor', 'r','MarkerSize',10)

plot(1.1818,1.1818,'--rs','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor',

'g','MarkerSize',10)

Page 7: MATLAB 5 Solution.pdf

plot(-2,-2,'--rs','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor',

'g','MarkerSize',10)

plot(-2,1.1818,'--rs','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor',

'b','MarkerSize',10)

text(0.4,0.36364,'(0.36, 0.36)','VerticalAlignment','top','HorizontalAlignment',

'center','FontWeight','bold')

text(-1.09,-1.1818, '(-1.18, -1.18)','VerticalAlignment','top',

'HorizontalAlignment','center','FontWeight','bold)

text(-0.35,-0.36364,'(-0.36,-36)','VerticalAlignment','top','HorizontalAlignment',

'center','FontWeight','bold')

text(1.2,-2,'(1.1818, -2)','VerticalAlignment','bottom','HorizontalAlignment',

'center','FontWeight','bold)

text(1.2,1.1818,'(1.1818,1.1818)','VerticalAlignment','top','HorizontalAlignment','

center','FontWeight','bold')

text(-1.9,1.1818,'(-2,1.1818)\leftarrow','HorizontalAlignment','left','FontWeight',

'bold')

text(-1.9,-2,'(-2,-2)\leftarrow','HorizontalAlignment','left','FontWeight','bold')

set(gca,'XTick',[-2:0.5:2],'XMinorTick','on','FontName','times','FontWeight',

'bold')

set(gca,'YTick',[-2:0.5:2],'YMinorTick','on','FontName','times','FontWeight',

'bold')

title({' ','15 labeled Contour curves f(x,y) = sin(3x + y) - 2cos(x - y)','over the

rectangle R = \{(x,y) | -2 \leq x \leq 2, -2 \leq y \leq 2\}','by Antony Foster'})

legend('saddle point','local min','local max','Location','NorthEastOutside')

xlabel('X-Axis','Color','red','FontName','mathematica','FontWeight','bold',

'FontSize',12)

ylabel('Y-Axis','Color','red','FontName','mathematica','FontWeight','bold',

'FontSize',12)

axis square axis([-2 2 -2 2]);

grid on

%%

Page 8: MATLAB 5 Solution.pdf

MATLAB OUTPUT

Comments: (THOUGH MY PICTURE IS A BIT CLUTTERED (TOO MANY LABELS!)) Points at the

blue markers are points where has its largest value locally. We see this in

the contour plot when we move closer and closer towards the blue markers the z

values (from the data cursor) increase and as you move away the z (data cursor)

decrease.

Points at the green markers are points where has its smallest value locally.

We see this in the contour plot when we move closer and closer towards the green

markers the z values (from the data cursor) decrease and as you move away the z

(data cursor) increase.

Points at the red markers are points where has neither a largest nor smallest

value locally. We see this in the contour plot where contour curves seem to

intersect.

-2 -1.5 -1 -0.5 0 0.5 1 1.5 2-2

-1.5

-1

-0.5

0

0.5

1

1.5

2

-2.6

-2.6

-2.6

-2.6

-2.6

-2.2

-2.2

-2.2 -2

.2

-2.2

-2.2

-2.2

-1.8

-1.8

-1.8-1

.8

-1.8

-1.8-1

.8-1.4

-1.4

-1.4

-1.4

-1.4

-1.4

-1.4

-1.4

-1

-1

-1

-1

-1-1-1

-1

-1

-0.6

-0.6

-0.6

-0.6

-0.6

-0.6

-0.6

-0.2

-0.2

-0.2

-0.2

-0.2

-0.2

-0.2

0.2

0.2

0.2

0.2

0.2

0.2

0.2

0.6

0.6

0.6

0.6

0.6

1

1

1 1

11

1

1.4

1.4

1.4

1.4

1.4

1.8

1.8

1.8

1.8

1.8

2.2 2

.2

2.2

2.2

2.6

2.6

(0.36, 0.36)

(-1.18, -1.18)

(-0.36,-36)

(1.1818, -2)

(1.1818,1.1818)(-2,1.1818)

(-2,-2)

15 labeled Contour curves f(x,y) = sin(3x + y) - 2cos(x - y)

over the rectangle R = {(x,y) | -2 x 2, -2 y 2}

by Antony Foster

X-Axis

Y-A

xis

saddle point

local min

local max

Page 9: MATLAB 5 Solution.pdf

MATLAB ASSIGNMENT ENDS HERE

NOTE: THIS IS NOT THE ANSWER TO PART A) or B). I just wanted to see more

contours curves (40 unlabeled ones) of so that I could better identify the

saddle points (I can see about 4 such points).

MATLAB CODE

clear all clc; clf reset figure(3)

f = @(x,y)sin(3*x + y) - 2*cos(x - y);

u = linspace(-2,2,45); v = linspace(-2,2,45); [x,y] = meshgrid(u,v);

contour(x,y,f(x,y),40,'k')

set(gca,'XTick',[-2:0.5:2],'XMinorTick','on','FontName','times','FontWeight',

'bold')

set(gca,'YTick',[-2:0.5:2],'YMinorTick','on','FontName','times','FontWeight',

'bold')

title({' ','40 unlabeled contour curves of f(x,y) = sin(3x + y) - 2cos(x -

y)','over the rectangle R = \{(x,y) | -2 < x < 2, -2 < y < 2\}','by Antony

Foster'})

xlabel('X-Axis','Color','red','FontName','mathematica','FontWeight','bold',

'FontSize',12)

ylabel('Y-Axis','Color','red','FontName','mathematica','FontWeight','bold',

'FontSize',12)

axis equal square axis([-2 2 -2 2]); grid on %%

Page 10: MATLAB 5 Solution.pdf

MATLAB OUTPUT

40 unlabeled contour curves of f(x,y) = sin(3x + y) - 2cos(x - y)

over the rectangle R = {(x,y) | -2 x 2, -2 y 2}

by Antony Foster

X-Axis

Y-A

xis

-2 -1.5 -1 -0.5 0 0.5 1 1.5 2-2

-1.5

-1

-0.5

0

0.5

1

1.5

2