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

Plotting of Curves and Surfaces

calculus
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Plotting of Curves and Surfaces

calculus
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

1.

PLOTTING OF CURVES AND SURFACES AND


SYMBOLIC COMPUTATION

Making 3D surface plots, contour plots, and gradient plots in Matlab is slightly more
complicated than making simple line graphs, but we will present some examples that, with
simple modifications, should enable you to create most of the pictures that you will need.

Matlab provides a variety of functions for displaying data as 2-D or 3-D graphics for 2-D
graphics; the command plots vector x1 versus vector y1, vector x2 versus vector y2, etc. on the
same graph. Other commands for 2-D graphics are: polar, bar, stairs, loglog, semilogx and
semilogy.

For 3-D graphics, the most commonly used commands are:

plot3(x1,y1,z1,’linestyle’, x1,y1,z1,’linestyle’,…..)

contour(x,y,z),mesh(x,y,z),surf(x,y,z)

The first statement is a 3-D analogue of plot() and plots lines and points in 3-D. the
second statement produces contour plots of the matrix z using the vectors x and y to control the
scaling on the x- and y- axes. For surface or meshplots, you use the third statement where x, y
are vectors or matrices and z is a matix. Other commands available for 3-D graphics are: pcolor,
image, contour3, fill3, cylinder, and sphere.

Mathematical form:
Importance visualizing curves and surfaces
 Draw the curve for the given function f(x)

 Draw the surface for the given function.

Symbolic Math in Matlab

 Matlab allows symbolic operations several areas such as Calculus, Differential Equations
and Transforms
MATLAB Syntax used:

Command Description
plot(y) Plots the columns of Y versus the index of each value when Y is a
real number. For complex Y, plot(Y) is equivalent to
plot(real(Y),imag(Y))
ezplot(fun) plots the expression fun(x) over the default domain -2π < x < 2π,
where fun(x) is an explicit function of only x.
ezplot(fun,[xmin,xmax]) plots fun(x) over the domain: xmin < x < xmax.
plot3(X1,Y1,Z1) Displays a three-dimensional plot of a set of data points
surf(Z) Creates a three-dimensional shaded surface from the z
components in matrix Z, using x = 1:n and
y = 1:m, where [m,n] = size(Z)
ezsurf(fun) Creates a graph of fun(x,y) using the surf function. Fun is plotted
over the default domain:
-2π < x < 2π, -2π < y < 2π.
Y = linspace(a,b,n) Generates a row vector y of n points linearly spaced between and
including a and b.
mesh(X,Y,Z) Draws a wireframe mesh with color determined by Z so color is
proportional to surface height
[X,Y] = meshgrid(x,y) Transforms the domain specified by vectors x and y into arrays X
and Y, which can be used to evaluate functions of two variables
and three-dimensional mesh/surface plots.
Subplot(m,n,p) Breaks the figure window into an m-by-n matrix of small axes,
selects the pth axes object for the current plot, and returns the axes
handle.
Colormap(map) Sets the colormap to the matrix map. If any values in map are
outside the interval [0 1], you receive the error Colormap must
have values in [0, 1].
Prism Repeats the six colors red, orange, yellow, green, blue, and violet

Flag Consists of the colors red, white, blue, and black. This colormap
completely changes color with each index increment

hsv. Varies the hue component of the hue-saturation-value color


model. The colors begin with red, pass through yellow, green,
cyan, blue, magenta, and return to red.
Shading flat Controls the color shading of surface and patch graphics objects.
subs(s,old,new) returns a copy of s, replacing all occurrences of old with new, and
then evaluates s.
limit(f,var,a) returns the Bidirectional Limit of the symbolic expression f when
var approaches a.
diff(f,var) differentiates f with respect to the differentiation parameter var.
var can be a symbolic scalar variable, such as x, a symbolic
function, such as f(x), or a derivative function, such as diff(f(t),t).

diff(f,var,n). computes the nth derivative of f with respect to var


int(expr,var) computes the indefinite integral of expr with respect to the
symbolic scalar variable var.

computes the definite integral of expr with respect to the symbolic


int(expr,var,a,b) scalar variable var from a to b.
expand(S) multiplies all parentheses in S, and simplifies inputs to functions
such as cos(x + y) by applying standard identities.

uses additional options specified by one or more name-value pair


expand(S,Name,Value) arguments.
simplify(expr) performs algebraic simplification of expr. If expr is a symbolic
vector or matrix, this function simplifies each element of expr.

Example 1:
Draw a circle with centre (1, 3)

MATLAB Code:
clc
clear all
t = linspace(0, 2*pi, 101);
x = 1 +2*cos(t);
y = 3 +2*sin(t);
plot(x,y,'r.')
axis equal
xlabel('x-axis')
ylabel('y-axis')
title('Circle')

Output:
In Figure Window
Circle
5

4.5

3.5
y-axis
3

2.5

1.5

1
-1.5 -1 -0.5 0 0.5 1 1.5 2 2.5 3 3.5
x-axis

Example 2:
Draw the graph by using without hold on function

MATLAB Code:
y = linspace(-10,10,1000)
plot(y,cos(y),'b.',y,cos(2*y),'g.')
xlabel('x axis')
ylabel('y axis')
legend('cos(x)','cos(2x)','location','northeast')
Output:
In Figure Window:

1
cos(x)
0.8 cos(2x)

0.6

0.4

0.2
y axis

-0.2

-0.4

-0.6

-0.8

-1
-10 -8 -6 -4 -2 0 2 4 6 8 10
x axis

Example 3:
Draw the surface by using plot3

MATLAB Code:
t=linspace(0,2*pi,500);
x=cos(t);
y=sin(t);
z=sin(5*t);
comet3(x,y,z)
plot3(x,y,z,'g*','markersize',7)
xlabel('x-axis')
ylabel('y-axis')
3D Curve
zlabel('z-axis')
title('3D Curve')
1

Output: 0.

In Figure window z-axis


0

-0.5

-1
1
0. 1
0 0.
0
-0.5 -0.5
y-axis -1 -1
x-axis
Example 4:
Draw the four curves sinx, cosx, e-x, sin3x in one window

MATLAB Code:
clc
clear all
x=0:.1:2*pi;
subplot(2,2,1);
plot(x,sin(x),'b*');
title('sin(x)')
subplot(2,2,2);
plot(x,cos(x),'r-o');
title('cos(x)')
subplot(2,2,3)
plot(x,exp(-x),'g.');
title('exp(-x)')
subplot(2,2,4);
plot(x,sin(3*x),'m-o');
title('sin(3x)')

Output:
In Figure Window
sin(x) cos(x)
1 1

0. 0.

0 0

-0.5 -0.5

-1 -1
0 2 4 6 8 0 2 4 6 8

exp(-x) sin(3x)
1 1

0.

0. 0

-0.5

0 -1
0 2 4 6 8 0 2 4 6 8

Example 5:
Draw the surface by using ezsurf and ezplot

MATLAB Code:
syms x y
f = 2*(x^2+y^2) 2 x2 + 2 y2

ezsurf(f)
colormap cool 150

Output: 100

50
In Figure Window:
0
5
5
0
0
-5 -5
y x

Example 6:
Draw the ezplot for the function x^2+2*x-6

MATLAB Code:
syms x
y = x^2+2*x-6
ezplot(y)
Output:
x2 + 2 x - 6
50
In the Figure window:
40

30

20

10

Example 7: 0

MATLAB Code: -10


-6 -4 -2 0 2 4 6
x
x=-1:.05:1;
y=-1:.05:1;
[x,y]=meshgrid(x,y);
z=x.*y.^2-x.^3
surf(x,y,z);
colormap spring
shading interp

Output:
In Figure window:

0.5

-0.5

-1
1
0.5 1
0 0.5
0
-0.5 -0.5
-1 -1
Example 8:

MATLAB Code:
df 2 x
Find , if f ( x )=x +cos ( 2 x ) +4 sin ( x ) + e
dx

syms x
f= x^2+cos(2*x)+4*sin(x)+exp(x)
diff(f,x) % differentiate f w.r.to x
diff(f,x,2)
Output: (In the command window)
ans = 2*x - 2*sin(2*x) + 4*cos(x) + exp(x)
ans = exp(x) - 4*cos(2*x) - 4*sin(x) + 2

Example 9:
3

∫ (3 x−x 2¿ ) d x ¿
0

MATLAB Code:
syms x
f= 3*x-x^2;
int(f,x,0,3)
Output: (In the command window)
ans = 9/2

You might also like