Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
12 ILLUSTRATIVE PROGRAMS
2.12.1 Task#1
Write a program to calculate area of circle having radius 3cm?
Program
r = 3;
Area = pi * (r ^ 2)
Output
Area =
28.
26
2.12.2 Task#2
Given two sides a=3.2 & b=4.6 of a triangle and angle 60 o between these
two sides, find the length of the third side and the area of the triangle?
𝑪
𝟏
𝑨𝒓𝒆𝒂 𝑶𝒇 𝑻𝒓𝒊𝒂𝒏𝒈𝒍𝒆 = 𝒂𝒃
𝐬𝐢𝐧(𝜽) 𝟐
Program
a=3.2;
b=6.4;
theta=60;
% Length of third side
c= sqrt(a^(2)+b^(2)-2*a*b*cos(theta))
% Area of circle in radians
Area= 0.5*a*b*sind(theta)
% Area of circle in degree
Area= 0.5*a*b*sin(theta)
Output
c =
9.4979
Area =
8.8681
Area =
-3.1213
2.12.3 Task#3
Write a program to convert temperature given in °C say 35.4°C to °F?
°F= 9/5°C+32
Program
C = 35.4;
F=(9/5*C+32)
Output
F =
95.7200
2.12.4 Task4#
Write MATLAB statement and calculate sum of series for x=1.5?
𝒙𝟐 𝒙𝟒 𝒙𝟔 𝒙𝟖
𝒔=𝟏− + − +
𝟐! 𝟒! 𝟔! 𝟖!
Program
x=1.5; s= 1-(x^2/factorial(2))+(x^4/factorial(4))-(x^6/factorial(6))+
(x^8/factorial(8)) Output
s=
0.0708
2.12.5 Task#5
Evaluate the following assignment statements.
%% x/(x^2+1)(sinx)
x=pi/4;
t=
(x./(x.^2+1).*sin(x)
) x=pi/2; u=
(x./(x.^2+1).*sin(x)
) Output
q =
14.3480
r =
0.5596 s
=
1.0986
t =
0.3435
u =
0.4530
2.12.6 Task#6
Evaluate the given number for ‘x’ from 1 to 2 in steps of 0.1.
𝟏 𝒙𝟑
𝒚 = 𝒙 + 𝒙𝟒 + 𝟓𝒙 𝐬𝐢𝐧 𝒙
Program
x=1:0.1:2;
y= (1./x)+((x.^3))./((x.^4)+(5.*x.*sin(x)))
Output
Columns 1 through 6
1.1920 1.1182 1.0587 1.0102 0.9698 0.9357
Columns 7 through 11
2.12.7 Task#7
Write assignment statements to evaluate the following equations:
𝑽𝑰 𝐜𝐨𝐬 𝜽
(C) 𝑬𝒇𝒇𝒊𝒄𝒊𝒆𝒏𝒄𝒚 = 𝑽𝑰 𝐜𝐨𝐬 𝜽 +𝑰𝟐 𝑹+𝝎𝒄 𝒇𝒐𝒓 𝑽 =
𝟐𝟓𝟎 , 𝑰 = 𝟓 , 𝑹 = 𝟐 , 𝐜𝐨𝐬 𝜽 = 𝟎. 𝟖 , 𝝎𝒄 = 𝟐𝟎
Program
%% Volume =
pi*r^2*h r=2;
h=3; v=
pi*(r^2)*h
%% Power= 2piNT/60
N= 1000; T=
10; p=
(2*pi*N*T)/6
0
%% Efficiency= VIcos(theta)/VIcos(theta)+I^2R+Wc
V=250;
I=5;
R=2;
theta=
0.8;
Wc=20;
e= V*I*cos(theta)/V*I*cos(theta)+I^2*R+wrightOmega(Wc)
Output
v =
37.699
1 p =
1.0472e+0
3 e =
79.2926
2.12.8 Task#8
For an electrical circuit with an inductance l=0.01mh and resistance r=180 ohms the
damped natural frequency of oscillations is:
𝟏 𝑹 𝟐
𝑭𝒓𝒆𝒒𝒖𝒆𝒏𝒄𝒚 √ 𝑳𝑪 − 𝟒𝑪𝟐
=
Write a program to calculate the frequency for different values of ‘c’ varying from 0.1 to 1
in step of 0.1.
Program
L=0.01;
R=180;
C=0.1:0.1:1;
F=sqrt((1./L.*C)+(R.^2)/4.*(C.^2))
Output
F =
Columns 1 through 9
9.5394 18.5472 27.5500 36.5513 45.5522 54.5527 63.5531
72.5534
81.5537
Column 10
90.5539
3 LAB#3
IMPLEMENTATION OF VECTORS
The learning objectives are:
• Knowledge of scalar, vectors and matrices and basic operations such as product and
transpose
• Useful commands/functions related to vectors and matrices
3.1 Introduction
The matrix notation usually simplifies the complex mathematical expressions /equations and
makes solution of problems much easier to handle and manipulate. In MATLAB, a matrix is a
rectangular array of real or complex numbers. Matrices with only one row or with only one
column are called row and column vectors, respectively. A matrix having only one element is
called a scalar.
Although other higher programming languages work with one number at a time, in MATLAB it
is possible to work with complete matrix simultaneously. This feature is very important as it
removes the un-necessary loops and repetition of same statements. The program, therefore,
becomes short, concise and easily understandable. In MATLAB, matrix is chosen as a basic data
element. All variables when used as a single data element are treated as single element matrix
that is a matrix with one row and one column.
3.2 Arrays
An array is a list of numbers arranged in rows and/or columns. A one-dimensional array is a row
or a column of numbers and a two dimensional array has a set of numbers arranged in rows and
columns. An array operation is performed element by element.