You are on page 1of 12

USE of linspace function and

incremental method to create a


vector A with n equally spaced
values.
Example: create a vector A with 15
equally spaced values beginning
with 7.0 and ending with 47.5.
This means that 7 is the initial
value, 47.5 is the final value and
15 is the number of values to be
displayed.

>> A=linspace(7,47.5,15)
A = Columns 1 through 9
7.0000 9.8929 12.7857
15.6786 18.5714 21.4643
24.3571 27.2500 30.1429
Columns 10 through 15
33.0357 35.9286 38.8214
41.7143 44.6071 47.5000

The increment method may also be used


The formula is:
increment = final value initial value

Number of increment 1
>> increment=(47.5-7)/(15-1)
increment = 2.8929
With this, 2.8929 is now assigned to be
the increment.

>> A=(7:increment:47.5)
A=
Columns 1 through 9
7.0000 9.8929 12.7857 15.6786 18.5714
21.4643 24.3571 27.2500 30.1429
Columns 10 through 15
33.0357 35.9286 38.8214 41.7143 44.6071
47.5000

Use linspace and apply the


increment method to create
vector B with starting
(initial) value of 7
and final (ending) value of 23
with increment of 0.16
between values. Display
only the 18th value in each
case.

Using the linspace function, we must find the


number of values that lie between 7 and 23.
The formula is:

>> Number = (23-7)/(0.16)+1


Number =
101

>> B=linspace(7,23,Number);
>> B(18)
ans =
9.7200
(b)
>> B=(7:0.16:23);
>> B(18)
ans =
9.7200 stop

Two and Three dimensional graphs


If x, y, and z are vectors with the same
size (length), they can be plotted using
the command plot3.
Plot3(x,y,z) generates a line in three
dimensions through the points whose
coordinates are the elements of x,y and
z and then produces a 2-dimensional
projection of that line on the screen.

>> t=(0:0.02*pi:25*pi);
>> x=sin(t); y=cos(t); z=t;
>> plot3(x,y,z)

80
60
40
20
0
1
0.5
0
-0.5
-1

-1

-0.5

0.5

Consider a scalar function of two


variables Z= f(x,y). This function
defines a surface in the threedimensional space. The surface can
be graphically represented using
mesh, meshc, surf. Surfc surf1 and
other commands and functions.
In general, the function Z = f(x,y) is
calculated and stored as the Z
matrix, and a grid is defined.
To evaluate the functions, meshgrid
is used. Here, the (x,y) data is
generated to evaluate Z = f(x,y).

We can calculate the nonlinear


functions

The x, y and z can be plotted using a


3-dimensional plot applying
meshgrid.
[x,y]=meshgrid [-4:0.1:4];
Z=x.*2*y.*exp(x.^2-y.^4);
Plot3(x,y,z)

You might also like