0% found this document useful (0 votes)
32 views9 pages

Matlab Vectors and Its Applications

The document discusses vector operations and applications in MATLAB. It provides 6 examples of problems involving vectors and matrix operations: 1) Calculating squares and summation of numbers from 1 to 20, 2) Calculating squares and summation of odd numbers from 1 to 20, 3) Calculating cubes and summation of even numbers from 1 to 20, 4) Calculating terms and summation of the Fibonacci series, 5) Plotting a sine curve, and 6) Calculating projectile range using velocity and angle vectors. Detailed step-by-step solutions and code are given for each problem.

Uploaded by

mrgaur gaur
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)
32 views9 pages

Matlab Vectors and Its Applications

The document discusses vector operations and applications in MATLAB. It provides 6 examples of problems involving vectors and matrix operations: 1) Calculating squares and summation of numbers from 1 to 20, 2) Calculating squares and summation of odd numbers from 1 to 20, 3) Calculating cubes and summation of even numbers from 1 to 20, 4) Calculating terms and summation of the Fibonacci series, 5) Plotting a sine curve, and 6) Calculating projectile range using velocity and angle vectors. Detailed step-by-step solutions and code are given for each problem.

Uploaded by

mrgaur gaur
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
You are on page 1/ 9

Vectors and its Applications

Lecture: 6 (Dr. Ajeet Kumar)

How to create vectors??

Method 1:
Syntax: v=1:1:10;

v=1:2:10

v = 1×5
1 3 5 7 9

v=1:3:10

v = 1×4
1 4 7 10

Method 2:
Syntax: v=linspace(1,10,10)

v=linspace(1,10,10)

v = 1×10
1 2 3 4 5 6 7 8 9 10

v=linspace(1,10,5)

v = 1×5
1 3.25 5.5 7.75 10

Problem: 1
Write a program to print square of the numbers between 1 to 20 and also calculate the summation of
these numbers.

Solution

Step 1

create the numbers between 1 to 20 (n is a row vector)

n=1:20

n = 1×20
1 2 3 4 5 6 7 8 9 10 11 12 13

1
Step 2

calculate the square of the numbers

N_square=n.^2

N_square = 1×20
1 4 9 16 25 36 49 64 81 100 121 144 169

Step 3

Summation of the squares of these numbers

N_sum=sum(N_square)

N_sum =
2870

Combined Code

% Solution of Problem 1
% Calculation of squares of the all the no. between 1 and 20 and their
% summation

n=1:20;
N_square=n.^2

N_square = 1×20
1 4 9 16 25 36 49 64 81 100 121 144 169

N_sum=sum(N_square)

N_sum =
2870

Problem: 2
Write a program to print square of all the odd numbers between 1 to 20 and also calculate the
summation of these numbers.

Solotion

Step 1

create all the odd numbers between 1 to 20 (n is a row vector)

n=1:2:20

n = 1×10

2
1 3 5 7 9 11 13 15 17 19

Step 2

calculate the square of the numbers

N_square=n.^2

N_square = 1×10
1 9 25 49 81 121 169 225 289 361

Step 3

Summation of the squares of these numbers

N_sum=sum(N_square)

N_sum = 1330

Combined Code

% Solution of Problem 2
% Calculation of squares of the all the odd no. between 1 and 20 and their
% summation
n=1:2:20;
N_square=n.^2
N_sum=sum(N_square)

Problem: 3
Write a program to print cube of all the even numbers between 1 to 20. Print all even numbers in one
column and their cube values in the next column. Also calculate the summation of cube of these
numbers.

Solution

Step 1

create all the even numbers between 1 to 20 (n is a row vector)

% n=2:2:20
n=2:2:20

n = 1×10
2 4 6 8 10 12 14 16 18 20

Step 2

calculate the square of these numbers

% N_cube=n.^3
N_cube=n.^3

3
N_cube = 1×10
8 64 216 512 1000 1728

Step 3

Arranging the even numbers and their respective squares column-wise and formring a Matrix for better
presentation of output.

n' = column vector, N_cube' =column vector having same numbers of rows (Adding column)

% table_cube=[n' N_cube']
% cube_summation=sum(N_cube)
table_cube=[n' N_cube']

table_cube = 10×2
2 8
4 64
6 216
8 512
10 1000
12 1728
14 2744
16 4096
18 5832
20 8000

cube_summation=sum(N_cube)

cube_summation = 24200

Combined Code

% Solution of Problem 3
% Write a program to print cube of all the even numbers between 1 to 20.
% Print all even numbers in one column and their square values in the next column.
% Also calculate the summation of squares of these numbers.

% n=2:2:20;
% N_cube=n.^3;
% table_cube=[n' N_cube']
% cube_summation=sum(N_cube)

Problem: 4
The (n+1)th term in the Fibonacci series is given as

; n =0,1,2,3,........

4
Display the first 10 terms of the series and find out the summation.

Solution

Step 1

create n from 0 to 9 in order to generate 10 terms of the series

% n=0:9
n=0:9

n = 1×10
0 1 2 3 4 5 6 7 8 9

Step 2

Define the term: Hint: split the bigger term into smaller term

T_n+1=(Tn_1-Tn_2)/sqrt(5) where Tn_1 and Tn_2 are the terms of the numerator

% Tn_1=((1+sqrt(5))/2).^n;
% Tn_2=((1-sqrt(5))/2).^n;
% T_n= (Tn_1-Tn_2)/sqrt(5)
Tn_1=((1+sqrt(5))/2).^n;
Tn_2=((1-sqrt(5))/2).^n;
T_n=(Tn_1-Tn_2)/sqrt(5)

T_n = 1×10
0 1.0000 1.0000 2.0000 3.0000 5.0000 8.0000 13.0000

Step 3

Summation of the terms

% Fib_sum=sum(T_n)
Fib_sum=sum(T_n)

Fib_sum = 88

Combined Code

% Problem 4
% Fibonacci series
% n=0:9;
% Tn_1=((1+sqrt(5))/2).^n;
% Tn_2=((1-sqrt(5))/2).^n;
% T_n= (Tn_1-Tn_2)/sqrt(5)
% Fib_sum=sum(T_n)

5
Problem 5:
Write a program to plot the sine curve between 0 to 2*pi.

Step 1:

Create a vector of theta between 0 to 2*pi

% th=linspace(0,2*pi,1000);
th=linspace(0,2*pi,100);

Step 2:

Calculate the sine value corresponding to these theta value and store them

% sin_th=sin(th);
sin_th=sin(th);

Step 3:

Use Matlab Plot option to show the variation of sine 

% plot(th,sin_th);
plot(th,sin_th)

6
 

Problem 6:
 

Output should be like this table 

Note: This is an example where one should be careful because the solution requires array and matrix
operation all together. 

From above table, it is clear that in order to fill the blank entries, we need an generate output "h" of 6X4
matrix.

6X4 matrix output can be get by performing matrix multiplication of velocity vector ( ) of 6x1 and angle
vector ( ) of 1x4.

Once this matrix "h" has been created, one can add entries corresponding to 1st row (angle input) and 1st
column (velocity input) as shown in above figure.

7
Thus one can conclude that above calculation needs both array and matrix operation (That why one should
very careful while using OPERATORS SIGN)

Step 1.
Defining constant and vectors as an input 

clear all
format short g
% g=9.8 % constant
% v=10:2:20 % velocity row vector, dim: 1x6
% th=50:10:80 % angle row vector, dim: 1x6
g=9.8;
v=10:2:20;
th=50:10:80;

Step 2:
Defining formula for h to calculate the matrix h of size 6x4

× =

Thus we need to convert velocity vector as column vector of 6x1

% h=((v'.^2)*(sind(th).^2))./(2*g)
h=((v'.^2)*(sind(th).^2))./(2*g)

h = 6×4
2.994 3.8265 4.5052 4.9482
4.3114 5.5102 6.4875 7.1254
5.8682 7.5 8.8302 9.6985
7.6646 9.7959 11.533 12.667
9.7006 12.398 14.597 16.032
11.976 15.306 18.021 19.793

Step 3:
Creating table

% tabulated_output_1=[0 th; v' h]


% tabulated_output_2=[[0; v'] [th; h]]
tabulated_output_1=[0 th;v' h]

tabulated_output_1 = 7×5
0 50 60 70 80
10 2.994 3.8265 4.5052 4.9482
12 4.3114 5.5102 6.4875 7.1254

8
14 5.8682 7.5 8.8302 9.6985
16 7.6646 9.7959 11.533 12.667
18 9.7006 12.398 14.597 16.032
20 11.976 15.306 18.021 19.793

tabulated_output_2=[[0;v'] [th;h] ]

tabulated_output_2 = 7×5
0 50 60 70 80
10 2.994 3.8265 4.5052 4.9482
12 4.3114 5.5102 6.4875 7.1254
14 5.8682 7.5 8.8302 9.6985
16 7.6646 9.7959 11.533 12.667
18 9.7006 12.398 14.597 16.032
20 11.976 15.306 18.021 19.793

You might also like