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

Lab(1-6) physics

Physics mechanics

Uploaded by

ayanrahman1187
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)
9 views

Lab(1-6) physics

Physics mechanics

Uploaded by

ayanrahman1187
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/ 38

EXPERIMENT: 1

OBJECTIVE 1 :

Write a function called circle that takes a scalar input r. It needs to return an output called area that
is the area of the circle with radius r and a second output cf that is the circumference of the same circle. You
are allowed to use the built in function pi. In fact, you need to use it to get the value of π as accurately as
possible.

SOFTWARE USED : MATLAB2023

THEORY:

Area of circle = 𝜋𝑟 2

Circumference of circle=2𝜋𝑟

CODE :

RESULT:
OBJECTIVE 2:

Write a function called even_index that takes a matrix, M, as input argument and returns a matrix
that contains only those elements of M that are in even rows and columns. In other words, it would return
the elements of M at indices (2,2), (2,4), (2,6), …, (4,2), (4,4), (4,6), …, etc. Note that both the row and the
column of an element must be even to be included in the output. The following would not be returned: (1,1),
(2,1), (1,2) because either the row or the column or both are odd. As an example, if M were a 5-by-8 matrix,
then the output must be 2-by-4 because the function omits rows 1, 3 and 5 of M and it also omits columns 1,
3, 5, and 7 of M.

SOFTWARE USED : MATLAB2023

THEORY:

Matrix is a set of numbers arranged in rows and columns so as to form a rectangular array. The
numbers are called the elements or entities of the matrix.

Indexing: Indexing on a matrix means selecting a subset of elements from itself. Indexing as a
function is used as a first or primary operation before many advanced operations. There are many types of
indexing possible as well, depending upon whether you want to select individual element out of a given
matrix or a set or range of elements.

CODE:

RESULT:
OBJECTIVE 3:

Creating a One and Two-Dimensional Array (Row / Column Vector) (Matrix of given size) then,
(A). Performing Arithmetic Operations - Addition, Subtraction, Multiplication and Exponentiation. (B).
Performing Matrix operations - Inverse, Transpose, Rank with plots.

SOFTWARE USED : MATLAB2023

THEORY:

MATLAB allows two different types of arithmetic operations-:

Matrix arithmetic operations

Matrix arithmetic operations are same as defined in linear algebra. Array operations are executed
element by element, both on one dimensional and multi-dimensional array.

ARITHMETIC OPERATIONS:

Addition (+): Addition or unary plus. A+B adds the values stored in variables A and B. A and B
must have the same size

Subtraction (-): Subtraction or unary minus. A-B subtracts the value of B from A. A and B must
have the same size.

Matrix Multiplication (*): Matrix multiplication. CAB is the linear algebraic product of the
matrices A and B. For non-scalar A and B, the number of columns of A must be equal to the columns of B.

Array Multiplication (.*): Array multiplication. A. *B is the element-by-element product of the


The matrix is called the inverse of X.

Inverse: A matrix X is invertible if there exists a matrix of the same size such that XY-YX. The
matrix is called the inverse of X.

Transpose: BA' returns the non-conjugate transpose of A, that is, interchanges the row and column
index for each element.

Rank: The number of linearly independent columns in a matrix is the rank of the matrix. The rank
gives a measure of the dimension of the range or column space of the matrix, which is the collection of all
linear combinations of the columns.

Commands:

Cle-Clears command windows

Clear all - Deletes variables, scripts in workspace.


Program:

Clc

Clear all

Creating ID arrays

a=[1 2 3]

b=[4 5 6]

c=[7;8;9]

Addition of 1D arrays

Sum=a+b

Subtraction of 1D arrays

Sub=a-b

Multiplication of ID arrays

Mul=a*c

Array element multiplication

Element mul=a.*b

Exponentiation of a matrix

expo_a a=exp(a)

Creating 2D arrays

a2=[2 1 2;7 8 9; 6 5 4]

b2=[2 2 2;3 3 3;4 4 4]

Addition of 2D arrays

c2=a2+b2

Subtraction of 2D arrays

c3=a2-b2

Multiplication of 2D arrays

c4=a2*b2

Inverse of Matrix

inversea2=inv(a2)
Transpose of Matrix

Transpose_a2=transpose(a2)

Rank of Matrix

Rank_a2=rank(a2)

RESULT:
EXPERIMENT: 2
OBJECTIVE 1 :

The equation of a straight line is y=mx+c, where m and c are constants . Compute the y-coordinate
of a line with slope m=0.5 and the intercept c=-2 at the following x- coordinates :
X= 0, 1.5, 3, 4, 5, 7, 9, and 10.

X= [0,1.5,3,4,5,7,9,10];
Y= 0.5*X -2

SOFTWARE USED : MATLAB2023

THEORY:

Straight line: A straight line in one dimension figure that does not have any breadth to it.

Equation of a straight line : y=mx+c

Where m is the slope and c is the intercept and both are constant.

RESULT:
OBJECTIVE 2 :

Create a vector t with 10 elements : 1,2,3,………,10. Now compute the following quantities:
X=tsin(t)
Y=(t-1)/(t+1)
Z= (sin(t2))/t2
Ans t=1:10;
X= t.*sin(t)
Y= (t-1)./(t+1)
Z= sin(t.^2)./(t.^2)

SOFTWARE USED : MATLAB2023

RESULT:
OBJECTIVE 3 :

All points with coordinates x= rcosθ and y=rsinθ, where r is constant ,lie on a circle with radius r,
i.e. they satisfy the equation x2+ y2= r2 . Create a column vector for θ with the values 0, π/4, π/2,
3π/4, π, and 5π/4. Take r=2 and compute the column vectors x and y . Now check that x and y
indeed satisfy the equation of a circle , by computing the radius
r= (x2+ y2)1/2
Ans . theta=[0;pi/4;pi/2;3*pi/4;5*pi/4]
r=2;
x=r*cos(theta)
y=r*sin(theta)
x.^2+y.^2

SOFTWARE USED : MATLAB2023

RESULT:
OBJECTIVE 4 :

Create a vector n of 11 elements from 0 to 10. Take r=0.5 and create another vector x= [r 0 r1
……….rn] with the x= r.^n command . Now take the sum of this vector with the command
s=sum(x) .

SOFTWARE USED : MATLAB2023

RESULT:
EXPERIMENT: 3
OBJECTIVE 1:

Performing Matrix Manipulations - Concatenating, Indexing, Sorting, Shifting, Reshaping,


Resizing and Flipping about a Vertical Axis / Horizontal Axis; Creating Arrays X & Y of given size (1 x N)
and Performing (A). Relational Operations - >, <=, >=, ~= (B). Logical Operations - ~, &, |, XOR

SOFTWARE USED : MATLAB2023

THEORY:

An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations. MATLAB is designed to operate primarily on whole matrices and arrays.
Therefore, operators in MATLAB work both on scalar and non-sealur data. MATLAB allows the
following types of elementary operations:

1. Arithmetic Operators

2. Relational Operators

3. Logical Operators

4. Bitwise Operations

5. Set Operations

6. Arithmetic Operators

MATLAB allows two different types of arithmetic operations:

• Matrix arithmetic operations

• Array arithmetic operations

Matrix arithmetic operations are same as defined in linear algebra. Array operations are executed
element by element, both on one-dimensional and multidimensional array.

The matrix operators and array operators are differentiated by the period (.) symbol. However, as
the addition and subtraction operation is same for matrices and arrays, the operator is same for
both cases. The following table gives brief description of the operators:
RELATIONAL OPERATORS
Relational operators can also work on both scalar and non-sealar data. Relational operators for
arrays perform element-by-element comparisons between two arrays and return a logical array of
the same size, with elements set to logical 1 (true) where the relation is true and elements set to
logical 0 (false) where it is not.
The following table shows the relational operators available in MATLAB:

LOGICAL OPERATORS
MATLAB offers two types of logical operators and functions:

• Element-wise-these operators operate on corresponding elements of logical arrays.

• Short-circuit-these operators operate on scalar, logical expressions.

Element-wise logical operators operate element-by-element on logical arrays. The symbols &
and are the logical array operators AND, OR, and NOT.
Short-circuit logical operators allow short-circuiting on logical operations. The symbols && and
are the logical short-circuit operators AND and OR.
BITWISE OPERATIONS
Bitwise operator works on bits and performs bit-by-hit operation. The truth tables for &, |, and ^
are as follows:

MATLAB provides various functions for bit-wise operations like bitwise and, bitwise or' and
hitwise not operations, shift operation, etc.
The following table shows the commonly used bitwise operations:

SET OPERATIONS
MATLAB provides various functions for set operations, like union, intersection and testing for
set membership, etc.
The following table shows some commonly used set operations:
MATLAB PROGRAM:
%generating two matrices% display('reshaping the matrix')
a=[2 4 3;5 6 8;9 8 7] g=reshape(h,2,6)
b=[2 5 8;5 9 4;4 7 5] % rotating a matrix%
% Concatenate two matrices% display(‘rotating a matrix')
display (‘horizontal concatenation') i=rot90(g)
c=[a b] % flipping a matrix%
display('vertical concatenation’) display('flipping a matrix')
c=[a;b] j=fliplr(i)
%indexing two matrices% % resizing a matrix%
display('indexing two matrices') display (‘resizing a matrix')
d=b(a) g(2,:)=[]
%sorting arrays g(:,2)=[]
display('sorting row wise') % shifting the matrix%
f=sort(a,1) display(‘shifting a matrix')
display('sorting column wise’) k=rand(4)
f=sort(a,2) l=circshift(k,[1,3])
% reshaping the matrix% %using relational operators(>, <,==
h=[2 3 4 5;8 1 2 0;6 9 3 7] <=, >=, ~=)%
x=5; %using logical operations%
display(‘using >= operator') m=[1 5;8 0]
x>=[9 2 3;4 5 6;7 8 10] n=[2 0;0 5]
display(‘using<= operator') display(‘using and operator’)
x<=[9 2 3;4 5 6;7 8 10] and(m,n)
display(‘using~= operator’) display(‘using not operator’)
x~=[9 2 3;4 5 6;7 8 10] ~m
display(‘using> operator’) display(‘using or operator’)
x>[9 2 3;4 5 6;7 8 10] or(m,n)
display(‘using< operator’) display(‘using xor operator’)
x<[9 2 3;4 5 6;7 8 10] xor(m,n)

OUTPUT:
OBJECTIVE 2:

Plot y=sin x, 0 ≤ x ≤ 2π, taking 100 linearly spaced points in the given interval. Label the axes
and put ‘Sine Plot’ in the title.

SOFTWARE USED : MATLAB2023

RESULT:
OBJECTIVE 3:

Make the same plot as above , but rather than displaying the graph as a curve , show the
unconnected data points . To display the data points with small circles , use plot (x, y,’o’). Two
plots can also be combined with the commands plot(x,y,x,y,’o’).

SOFTWARE USED : MATLAB2023


RESULT:
OBJECTIVE 4:

Plot y=e-0.4x sinx, 0 ≤ x ≤ 4π, taking 10, 50, and 100 points interval.

SOFTWARE USED : MATLAB2023

RESULT:
OBJECTIVE 5:

Use the command plot3(x,y,z)to plot the circular helix x(t)=sin t, y(t)= cos t, z(t)=t, 0≤ t≤
20.

SOFTWARE USED : MATLAB2023

RESULT:
EXPERIMENT: 4
OBJECTIVE :

Evaluating a given expression and rounding it to the nearest integer value using Round,
Floor, Ceil and functions ; Also, generating and plots of

(A) Trigonometric functions – sin(t), cos(t), tan(t), sec(t), cosec(t) and cot(t) for a given duration ‘t’.
(B) Logarithmic and other functions – log(A), log10(A), Square root of A, Real nth root of A.

SOFTWARE USED : MATLAB2023


RESULT:
EXPERIMENT: 5
OBJECTIVE :

Creating a vector X with elements, Xn = (-1)n+1/(2n-1) and Adding up 100 elements of


the vector, X; And, plotting the functions, x, x3, ex, exp(x2) over the interval 0 < x < 4 (by choosing
appropriate mesh values for x to obtain smooth curves), on A Rectangular Plot

SOFTWARE USED : MATLAB2023


RESULT:
EXPERIMENT: 6
OBJECTIVE :

Generating a Sinusoidal Signal of a given frequency with Titling, Labeling, Adding Text, Adding
Legends, Printing Text in Greek Letters, Plotting as Multiple and Subplot. Time scale the generated signal
for different values. E.g. 2X, 4X, 0.25X, 0.0625X.

SOFTWARE USED : MATLAB2023


RESULT:

You might also like