0% found this document useful (0 votes)
3 views20 pages

Vector Calculus Lab2

The document provides commands and examples for plotting vector and scalar fields using Scilab. It includes functions for creating grids, plotting 3D surfaces, and visualizing vector fields with various mathematical functions. Additionally, it covers the gradient and divergence of scalar and vector fields with corresponding code snippets.

Uploaded by

kumarshashwat888
Copyright
© All Rights Reserved
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)
3 views20 pages

Vector Calculus Lab2

The document provides commands and examples for plotting vector and scalar fields using Scilab. It includes functions for creating grids, plotting 3D surfaces, and visualizing vector fields with various mathematical functions. Additionally, it covers the gradient and divergence of scalar and vector fields with corresponding code snippets.

Uploaded by

kumarshashwat888
Copyright
© All Rights Reserved
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

Engineering Mathematics II

Scilab
Vector Calculus Lab 2
Commands for plotting Vector Fields and Scalar Fields
Description Code Example:
Creating Grids in the space meshgrid(x,y), x=linspace(-5,5,100)
Note: ndgrid() works for creating grids OR y=linspace(-5,5,100)
with any number of variables. ndgrid(x,y,z,t) [X,Y]=meshgrid(x,y)
[X,Y,Z,T]= ndgrid(x,y,z,t)
x,y,z,t are vectors.
Plotting 3D surfaces surf(x,y,Z) x=linspace(-5,5,100)
y=linspace(-5,5,100)
x,y are vectors. [X,Y]=meshgrid(x,y)
Z is a matrix. Z=Y.^2 + X.^2
surf(x,y,Z)

Plotting vector field champ(x,y,Zx,Zy,a,rect=[-2,-2,2,2]) x=-4:.1:4


x,y are vectors. y=-4:.1:4
Zx, and Zy are matrices. [X,Y]=ndgrid(x,y)
‘a’ gives the size of the head of the Zx=2*X + Y +10
arrow Zy=2*Y + X +10
rect=[] is a rectangle in which a vector champ(x,y,Zx,Zy,.7,rect=[-1,-1,1,1])
field is plotted.
Plotting of a Scalar Field 𝒇 𝒙, 𝒚 = 𝒙𝟐 𝒚
clc; clear; clf;
function z=scalarfield(x, y)
z=(x.^2).*y
endfunction
x=linspace(-5,5,100)
y=linspace(-5,5,100)
[X,Y]=meshgrid(x,y)
z=scalarfield(X,Y)
surf(x,y,z)
xtitle('Scalar field’, ’X-Axis’, ’Y-Axis’)
Colorbar
--------------------------------------------------------------------------------------------------------------
Output:
Plotting of a Scalar Field 𝒇 𝒙, 𝒚 = 𝒙𝒆 !(𝒙𝟐 $𝒚𝟐 )

clc; clear; clf;


function z=scalarfield(x, y)
z=x.*exp(-x.^2-y.^2)
endfunction
x=linspace(-4,4,100)
y=linspace(-4,4,100)
[X,Y]=meshgrid(x,y)
z=scalarfield(X,Y)
surf(x,y,z)
xtitle('Scalar field','X-Axis','Y-Axis’);
Colorbar
---------------------------------------------------------------------------------------------------
Output:
Plotting of a Vector Field 𝒇 𝒙, 𝒚 = (𝒙, 𝒚)
clc; clear; clf;
function [Zx, Zy]=vfield(x, y)
Zx=x
Zy=y
endfunction
x=-4:.1:4; y=-4:.1:4;
[X,Y]=ndgrid(x,y)
[Zx,Zy]=vfield(X,Y)
champ(x,y,Zx,Zy,0.2,rect=[-2,-2,2,2])
gce().colored="on"
xtitle('Vector field f(x,y)=(x,y)','X-Axis','Y-Axis’);
Colorbar
---------------------------------------------------------------------------------------------------------
Output:
Plotting of a Vector Field 𝒇 𝒙, 𝒚 = (−𝒚, 𝒙)
clc; clear; clf;
function [Zx, Zy]=vfield(x, y)
Zx=-y
Zy=x
endfunction
x=-4:.1:4
y=-4:.1:4
[X,Y]=ndgrid(x,y)
[Zx,Zy]=vfield(X,Y)
champ(x,y,Zx,Zy,0.2,rect=[-2,-2,2,2])
gce().colored="on"
xtitle('Vector field f(x,y)=(-y,x)','X-Axis','Y-Axis’);
Colorbar
---------------------------------------------------------------------------------------------------------------------
Output:
Plotting of Vector Field 𝒇 𝒙, 𝒚 = (𝒔𝒊𝒏 𝒚, 𝒄𝒐𝒔 𝒙)
clc clear clf
function [Zx, Zy]=vfield(x, y)
Zx=sin (y)
Zy=cos (x)
endfunction
x=linspace(-1,1,100)*2*%pi
y=linspace(-1,1,100)*2*%pi
[X,Y]=ndgrid(x,y)
[Zx,Zy]=vfield(X,Y)
champ(x,y,Zx,Zy,0.5,rect=[-3,-2,3,2])
gce().colored="on"
xtitle('Vector field f(x,y)=(sin y, cos x)','X-Axis','Y-Axis’);
Colorbar
------------------------------------------------------------------------------------------------------------
Output:
The gradient of a scalar Field 𝒇 𝒙, 𝒚 = 𝒙 𝒚 𝟐
clc; clear; clf;
function [z, DZx, DZy]=scalarfield(x, y)
z=x.^2.*y
DZx=2*x.*y
DZy=x.^2
endfunction
x=linspace(-5,5,100); y=linspace(-5,5,100)
[X,Y]=meshgrid(x,y); [z,DZx,DZy]=scalarfield(X,Y)
Surf(x,y,z)
xtitle(‘scalar field f(x,y)=x^2y','X-Axis','Y-Axis’); scf
champ(x,y,DZx,DZy,0.5,rect=[-3,-3,3,3])
gce().colored="on"
xtitle(‘Gradient of scalar field f(x,y)=x^2y','X-Axis','Y-Axis’);
Colorbar
--------------------------------------------------------------------------------------------------------------
Output:
The gradient of Scalar Field 𝒇 𝒙, 𝒚 = 𝒙𝒆 "(𝒙𝟐 %𝒚𝟐 )
clc clear clf
function [z, DZx, DZy]=scalarfield(x, y)
z=x.*exp(-x.^2-y.^2); DZx=exp(-x.^2-y.^2) -2*x.^2.*exp(-x.^2-y.^2)
DZy=-2*x.*y.*exp(-x.^2-y.^2)
endfunction
x=linspace(-4,4,100) y=linspace(-4,4,100)
[X,Y]=meshgrid(x,y)
[z,DZx,DZy]=scalarfield(X,Y)
surf(x,y,z)
xtitle('Scalar field','X-Axis','Y-Axis'); colorbar scf
champ(x,y,DZx,DZy,0.3,rect=[-2,-2,2,2]) gce().colored="on" xtitle('Vector field','X-Axis','Y-
Axis'); colorbar
-----------------------------------------------------------------------------------------------------
Output:
Divergence of vector field 𝒇 𝒙, 𝒚 = (−𝒚, 𝒙)
clc clear clf
function [Zx, Zy, Div]=vfield(x, y)
Zx=-y; Zy=x; Div=0*x
endfunction
x=-4:.1:4; y=-4:.1:4; [X,Y]=meshgrid(x,y)
[Zx,Zy,Div]=vfield(X,Y)
surf(x,y,Div)
xtitle('Divergence of Vector field f(x,y)=(y,x)','X-Axis','Y-Axis'); colorbar scf;
champ(x,y,Zx,Zy,0.2,rect=[-2,-2,2,2]) gce(); colored="on" xtitle('Vector field
f(x,y)=(-y,x)','X-Axis','Y-Axis’);
Colorbar
-------------------------------------------------------------------------------------------
Output:
Divergence of vector field 𝒇 𝒙, 𝒚 = (𝒔𝒊𝒏𝒙, 𝒄𝒐𝒔𝒚)
clc clear clf
function [Zx, Zy, Div]=vfield(x, y)
Zx=sin (x) Zy=cos (y) Div=cos(x) - sin(y)
endfunction
x=linspace(-1,1,50)*%pi; y=linspace(-1,1,50)*%pi
[X,Y]=ndgrid(x,y)
[Zx,Zy,Div]=vfield(X,Y)
surf(x,y,Div)
xtitle('Divergence of Vector field f(x,y)=(sin x, cos y)','X-Axis','Y-Axis'); colorbar; scf;
champ(x,y,Zx,Zy,0.2,rect=[-3,-%pi,2.5,%pi]); gce().colored="on" xtitle('Vector field
f(x,y)=(sin x, cos y)','X-Axis','Y-Axis’);
Colorbar
---------------------------------------------------------------------------------------------------
Output

You might also like