0% found this document useful (0 votes)
159 views

MATLAB 5 Solution PDF

The document contains a MATLAB assignment submitted by Antony Foster for a Math course. It includes scripts and outputs for contour plots of two surfaces defined over square regions to identify any critical points. For the first surface f(x,y)=sin(3y-x^2+1)+cos(2y^2-2x), labeled contour plots identify three saddle points. For the second surface f(x,y)=sin(3x+y)-2cos(x-y), labeled and unlabeled contour plots identify four critical points, determined to be two minima, one maximum and one saddle point based on contour behavior near these points.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
159 views

MATLAB 5 Solution PDF

The document contains a MATLAB assignment submitted by Antony Foster for a Math course. It includes scripts and outputs for contour plots of two surfaces defined over square regions to identify any critical points. For the first surface f(x,y)=sin(3y-x^2+1)+cos(2y^2-2x), labeled contour plots identify three saddle points. For the second surface f(x,y)=sin(3x+y)-2cos(x-y), labeled and unlabeled contour plots identify four critical points, determined to be two minima, one maximum and one saddle point based on contour behavior near these points.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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

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 bounded) region, S, given by over the (closed and

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')

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
10 labeled Contour curves of the surface f(x,y) = sin(3y - x 2 + 1) + cos(2y2 - 2x) over the rectangle S = {(x,y) | -2 x 2, -1 y 1 } by Antony Foster 1 -1.5 0 .5 1 1
0.5 0
1

-1

0 -0 . 5

This is a saddle point

0.5

1.5

-1 . 5

0 is a saddle point This 0.5 1 1.5

Y-Axis

-1 .5
5 -0 .
-1
0

1.5

-0.5
.5 0 -1 -0 .5 0

0 -0 .5
1.5

5 0.

This is a saddle point

-1 -2

-1 .5

0.5 1

-1.5

-1

-0.5

0.5

1.5

X-Axis

-0 .5

-1 .5 -1

0.5 -0 . 0 -1 5

-1 . 5

0 -0 .5 -1 .5 0

.5

-0 .5

-1 1 0.5 0 -0 .5

5 -1 . 5 -1 -0 .0
1

-1 5 -1 .
-1 .5 -1

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 %%

MATLAB OUTPUT

40 unlabeled contour curves of f(x,y) = sin(3y - x 2 + 1) + cos(2y2 - 2x) over the rectangle R = {(x,y) | -2 x 2, -1 y 1 } by Antony Foster 1 0.75 0.5 0.25

Y-Axis

0 -0.25 -0.5 -0.75 -1 -2 -1.5 -1 -0.5 0 0.5 1 1.5 2

X-Axis

Exercise 5.3 Consider the surface given by


A) Write a script M-file that will produce square region given by

. labeled contour curves for over the (closed and bounded) .

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)

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 %%

MATLAB OUTPUT
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 22 8
1. 4 1 -0 .20.2 -0 .6 -1 .8
.2

1.

1.5 1 0.5

6 -2 .

saddle point local min local max

4 -1..8 -1 .2 -2

0.61

-1

-0 .6

(1.1818,1.1818)

-1

-2 .6 -2 .2

0.2 (-2,1.1818) -0.2

-0 .20.2 -0 .6 -1 .8 -1 .4

2.2 4 1.8 1. 0 1 0.6


-1

-1

-2 .2

-1

-0.5 -1 -1 . -1 8 -1.5

(0.36, 0.36) -1 .8 .4 -1 -1 -0 .6 -0 .2 0.2 (-0.36,-36) 0.6

Y-Axis

.2 -2

-1 .4

.4 (-1.18, -1.18)
-0 .6 -1 -0.2 0.2

- .2-0 .6 0.6 0.0 2 1.4 2.2 1.8

-1 .8

-1

-2 (-2,-2) -2 -1.5 -1

(1.1818, -2)

-0.5

0.5

1.5

X-Axis

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.

1
1.4 1.8 2.2 2.6

-1 0 .6 -0 .0. 22

1.4 1.8

0.6

2.6

-2 .2 -1 .4

.4 -1 8 -1 . 6 . -2

-2 .6 -2.2

-1 .4

2 -2 . .6 -2

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 %%

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 2 1.5 1 0.5

Y-Axis

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

X-Axis

You might also like