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

1-2

Physics

Uploaded by

riyadhassanbd12
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)
10 views

1-2

Physics

Uploaded by

riyadhassanbd12
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/ 31

1. Write a program that takes the center, radius the circle and takes any point.

Show
that the point is inside of the circle or outside of the circle or the point on the circle.

Input
clc
clear all
disp('please give the centre')
x1=input('x1=');
y1=input('y1=');
disp('please give the radius')
r=input('r1=');
disp('please give any point')
x2=input('x2=');
y2=input('y2=');
disp('distance of centre and that point')
d=sqrt((x2-x1).^2+(y2-y1).^2)
if d>r
disp('the point is outside of the circle ')
elseif d<r
disp('the point is inside of the circle ')
else
disp('the point is on the circle ')
end
Output
please give the centre

x1=0

y1=0

please give the radius

r1=1

please give any point

x2=3

y2=2

distance of centre and that point

d =3.6056

the point is outside of the circle .


2. Write a program that takes three vertices of a triangle and also takes any point, and
display whether the point is outside or inside.

Input

clc
clear all
disp('Enter the first vertex A ')
x1=input('x1=');
y1=input('y1=');
disp('Enter the second vertex B ')
x2=input('x2=');
y2=input('y2=');
disp('Enter the third vertex C ')
x3=input('x3=');
y3=input('y3=');
disp('Enter any point P ')
x4=input('x4=');
y4=input('y4=');
r=.5*[x1 y1 1;x2 y2 1;x3 y3 1];
r1= det(r);
r2=abs(r1);
a=.5*[x1 y1 1;x4 y4 1;x2 y2 1];
a1=det(a);
a2=abs(a1);
b=.5*[x1 y1 1;x4 y4 1;x3 y3 1];
b1=det(b);
b2=abs(b1);
c=.5*[x2 y2 1;x4 y4 1;x3 y3 1];
c1=det(c);
c2=abs(c1);
if r2==a2+b2+c2
disp('The given point is in the triangle ')

else
disp('The given point is outside of the triangle ')
end

Output
Enter the first vertex A

x1=2

y1=0

Enter the second vertex B

x2=0

y2=0

Enter the third vertex C

x3=0

y3=2

Enter any point P

x4=1

y4=1

The given point is in the triangle.

3. Create a program that takes three numbers, find the maximum and minimum
number and also find the average of maximum & minimum numbers and difference
between maximum & minimum number.

Input

clc
clear all
a=input('a= ');
b=input('b= ');
c=input('c= ');
if a==b&&b==c
disp('the given three numbers are equal')
else
if a>=c&&a>=b
fprintf('%d is a maximum number\n\n',a)
max=a;
elseif b>=a&&b>=c
fprintf('%d is a maximum number\n\n',b)
max=b ;
else
fprintf('%d is a maximum number\n\n',c)
max=c;
end

if a>=c&&b>=c
fprintf('%d is a minimum number\n\n',c)
min=c;
elseif a>=b&&c>=b
fprintf('%d is a minimum number\n\n',b)
min=b;
else
fprintf('%d is a minimum number\n\n',a)
min=a;
end
end
average=(min+max)/2
difference=max- min

Output

If,
a= 3
b= 6
c= 8
8 is a maximum number

3 is a minimum number

average = 5.5000
difference = 5
4. Create a program that takes n numbers and find the maximum and minimum
number.

Input

clc
clear all
n=input('n= ');
for i=1:n
disp('enter the number')
a(i)=input('');
end
max=a(1);
for i=1:n
if a(i)>=max
max=a(i);

end
end

fprintf('%d is maximum value\n\n',max)


min=a(1);
for i=1:n
if a(i)<=min
min=a(i);
end
end
fprintf('%d is minimum value\n\n',min)

Output

If
n= 4
Enter the number
2
Enter the number
3
Enter the number
5
Enter the number
67

Then 67 is maximum value


& 2 is minimum value
5. Write a program that takes any number and find factorial of this number.

Input

clc
clear all
n=input('n= ');
sum=0;
fac=1;
for i=1:1:n
sum=sum+i;
fac=fac*i;
end
fprintf('the required summation is %d\n\n And the required factorial is %d\n\n',sum,fac)

Output

If
n= 9, then
the required summation is 45

And the required factorial is 362880.

6. Write a program that takes any number n and find the Fibonacci series up to nth
terms.

Input

clc
clear all
n=input('n= ');
a=0;
b=1;
for i=1:1:n
fprintf('%d\n\n',a)
c=a+b;
a=b;
b=c ;
end

Output
If n= 10, Then
0

13

21

34

7. Write a program that, takes n random numbers and find the acceding order of the
numbers.

Input
clc
clear all
n=input('n= ');
for i=1:n
disp('enter the number ')
a(i)=input('');
end
for i=1:n
for j=i+1:n
if a(i)>=a(j)
temp=a(i)
a(i)=a(j)
a(j)=temp
end
end
fprintf('%d\n\n',a(i))
end
Output

n= 6
enter the number
2
enter the number
6
enter the number
9
enter the number
5
enter the number
8
enter the number
4
……….

8. Write a program that takes n random numbers and find the descending order of the
number.

Input

clc
clear all
n=input('n= ');
for i=1:n
disp('enter the number ')
a(i)=input('');
end
for i=1:n
for j=i+1:n
if a(i)<=a(j)
temp=a(i);
a(i)=a(j);
a(j)=temp;
end
end
fprintf('%d\n\n',a(i))
end

Output

n= 6
enter the number
2
enter the number
3
enter the number
5
enter the number
8
enter the number
0
enter the number
3
…………
8

0
9. Write a program that takes n random numbers and find a specific number’s
position of this taken numbers.

Input

clc
clear all
n=input('n= ');
for i=1:n
disp('enter the number ')
a(i)=input('');
end
v=input('enter your finding value ');
count=0;
for i=1:n
if a(i)~=v
count=count+1;

else
fprintf('the finding values position is %d\n\n',i)

end
end
if count==n
disp('the value is absent')
end

Output

n= 5
enter the number
2
enter the number
4
enter the number
6
enter the number
8
enter the number
6
enter your finding value 8
the finding values position is 4
Matrix related problem

10. Create a row vector that has the elements : 32,4,81,e^2.5,63,cos(pi/3),and 14.12.

Input
clc
clear all
A=[ 32 4 81 exp(2.5) 63 cos(pi/3) 14.12]

Output

A=

32.0000 4.0000 81.0000 12.1825 63.0000 0.5000 14.1200

11. Create a column vector that has the elements :55,14,In(51),987,0,and 5sin((2.5(pi))

Input

clc
clear all
A=[ 55 14 log(51) 987 0 5*sin(2.5*pi)]'

Output

A=

55.0000
14.0000
3.9318
987.0000
0
5.0000

12. Create a row vector in which the first element is 1,the last is 33,with an increment of
2 between the elements (1,3,5,…..,33)

Input

clc
clear all
A=[ 1:2:33]
Output

A=
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33

13. Create a column vector in which the first element is 15,the elements decrease with
increments of -5,and the last element is-25.(A column vector can be created by the
transpose of a row vector )

Input
clc
clear all
A=[ 15:-5:-25]’

Output

A=

15
10
5
0
-5
-10
-15
-20
-25

14. Create a row vector with 15 equally spaced elements in which the first element is 7
and the last element is 40.

Input

clc
clear all
A=[ linspace(7,40,15)]
Output

A=

Columns 1 through 9

7.0000 9.3571 11.7143 14.0714 16.4286 18.7857 21.1429 23.5000 25.8571

Columns 10 through 15

28.2143 30.5714 32.9286 35.2857 37.6429 40.0000

15. Create a column vector with 12 equally spaced elements in which the first element
is-1 and the last element is-15.

Input

clc
clear all
A=[linspace(-1,-15,12)]'

Output

A=

-1.0000

-2.2727

-3.5455

-4.8182

-6.0909

-7.3636

-8.6364

-9.9091

-11.1818

-12.4545

-13.7273

-15.0000
16. Create a vector name it Afirst,that has 16 elements in which the first is 4,the
increament is 3 and the last element is 49.then,using the colon symbol,create a new
vector,call it Asecond,that has eight elements.the first four elements are the first
four elements of the vector Afirst ,and the last four are the last four elements of the
vector Afirst.

Input

clc
clear all
Afirst=[4:3:49]
Asecond=[Afirst([1 2 3 4]) Afirst([13 14 15 16])]

Output

Afirst =

Columns 1 through 15

4 7 10 13 16 19 22 25 28 31 34 37 40 43 46

Column 16

49

Asecond =

4 7 10 13 40 43 46 49

17. Create the matrix shown below by using the vector notation for creating vectors
with constant spacing and/or the linspace command when entering the rows.
B=

Input

clc
clear all
B=[1:3:25; 72:-6:24; 0:0.125:1.000]

Output

B=

1.0000 4.0000 7.0000 10.0000 13.0000 16.0000 19.0000 22.0000 25.0000

72.0000 66.0000 60.0000 54.0000 48.0000 42.0000 36.0000 30.0000 24.0000

0 0.1250 0.2500 0.3750 0.5000 0.6250 0.7500 0.8750 1.0000

18. CR EATE THE FOLLO W IN G M ATR IX A:

A =

US E THE M ATR IX A TO :

a) Create a five -element row vector named va that contains the elements of the second
row of A .

b) Create a three-element row vector named vb that contains the elements of the
fourth column of A.

c) Create a ten –element row vector named vc that contains the elements of the first
second row of A

d) Create a six element row vector named vd that contains the elements of the second
and fifth columns of A.

Input
clc
clear all
A=[6 43 2 11 87; 12 6 34 0 5; 34 18 7 41 9]
va=A(2,:)
vb=[A(:,4)]'
vc=[A(1,:) A(2,:)]
vd=[[A(:,2)]' [A(:,5)]']

Output

A=

6 43 2 11 87

12 6 34 0 5

34 18 7 41 9

va =

12 6 34 0 5

vb =

11 0 41

vc =

6 43 2 11 87 12 6 34 0 5

vd =

43 6 18 87 5 9

19. CR EATE THE FOLLO W IN G M ATR IX C :

C=
US E THE M ATR IX C TO:

a) Create a three-element column vector named ua that contains the elements of the
third column of C

b) Create a five- element column vector named ub that contains the elements of the
second row of C

c) Create a nine-element column vector named uc that contains the elements of the
first,third and fifth columns of C .

d)Create a ten-element column vector named ud that contains the elements of the
first and second rows of C.

Input

clc
clear all
C=[2:2:10; 3:3:15; 7:7:35]
ua=C(:,3)
ub=[C(2,:)]'
uc=[[C(:,1)]' [C(:,3)]' [C(:,5)]']'
ud=[C(1,:) C(2,:)]'

Output

C=

2 4 6 8 10

3 6 9 12 15

7 14 21 28 35

ua =

21
ub =

12

15

uc =

21

10

15

35

ud =

10

12

15
20. CR EATE THE FOLLO W IN G M ATR IX A:

A =

1 2 3 4 5 6 7

2 4 6 8 10 12 14

21 18 15 12 9 6 3

5 10 15 20 25 30 35

a) Create a 3x4 matrix B from the 1st,3rd,and 4th rows, and the 1st ,3rd through 5th , and
7th columns of the matrix A.

b) Create a 15 elements-long row vector u from the elements of the third row,and the
5th and 7th columns of the matrix A.

Input

clc
clear all
A=[1:1:7;2:2:14;21:-3:3;5:5:35]
B=A([1,3,4],[1,3:5,7])
U=[A(3,:)'; A(:,5); A(:,7)]'

Output

A=

1 2 3 4 5 6 7
2 4 6 8 10 12 14
21 18 15 12 9 6 3
5 10 15 20 25 30 35

B=

1 3 4 5 7
21 15 12 9 3
5 15 20 25 35
U=

Columns 1 through 13

21 18 15 12 9 6 3 5 10 9 25 7 14

Columns 14 through 15

3 35

21. using the zeros,ones,and eye commands create the following arrays:

a= 0 0 0 0 0

0 0 0 0 0

b= 1 0 0 0

0 1 0 0

0 0 1 0

0 0 0 1

c= 1 1

1 1

1 1

Input

clc
clear all
a=zeros(2,5)
b=eye(4,4)
c=ones(3,2)

Output
a=

0 0 0 0 0
0 0 0 0 0

b=

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

c=

1 1
1 1
1 1

22. using the eye command create the array A shown on the left below. Then, using the
colon to address the elements in the array , change the array to be like the one
shown on the right.

A= 1 0 0 0 0 0 0

0 1 0 0 0 0 0

0 0 1 0 0 0 0

0 0 0 1 0 0 0

0 0 0 0 1 0 0

0 0 0 0 0 1 0

0 0 0 0 0 0 1
A=

2 2 2 0 5 5 5
2 2 2 0 5 5 5
3 3 3 0 5 5 5
0 0 0 1 0 0 0
4 4 7 0 9 9 9
4 4 7 0 9 9 9
4 4 7 0 9 9 9

Input

clc
clear all
A=eye(7)
A(1:2,1:3)=[2];
A(3,1:3)=[3];
A(5:7,1:2)=[4];
A(5:7,3)=[7];
A(1:3,5:7)=[5];
A(5:7,5:7)=[9]

Output

A=

1 0 0 0 0 0 0
0 1 0 0 0 0 0
0 0 1 0 0 0 0
0 0 0 1 0 0 0
0 0 0 0 1 0 0
0 0 0 0 0 1 0
0 0 0 0 0 0 1
A=

2 2 2 0 5 5 5
2 2 2 0 5 5 5
3 3 3 0 5 5 5
0 0 0 1 0 0 0
4 4 7 0 9 9 9
4 4 7 0 9 9 9
4 4 7 0 9 9 9

23. Using the zeros and ones commands create a 3x5 matrix in which the
first,second,and fifth columns are 0’s,and the third and fourth columns are 1’s.

Input

clc
clear all
A=zeros(3,5)
A(:,3:4)=ones(3,2)

Output

A=

0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

A=

0 0 1 1 0
0 0 1 1 0
0 0 1 1 0

24.Create a 5 x 7 matrix in which the first row are the numbers 1 2 3 4 5 6 7 ,andsecond
row are the numbers 8 9 10 11 12 13 14 , the third row are the numbers 15 through
21,and so on.from the this matrix create a new 3 x 4 matrix that is made from rows 2
through 4,and columns 3 through 6 of the first matrix .
Input

clc
clear all
A=[1:1:7; 8:1:14; 15:1:21; 22:1:28; 29:1:35]
B=[A(2:4,3:6)]

Output
A=

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31 32 33 34 35

B=

10 11 12 13

17 18 19 20

24 25 26 27

25. Create a 3 x 3 matrix A in which all the elements are 1, and create a 2 x 2 matrix B
in which all the elements are 5.then , add elements to the matrix A by appending the
matrix B such that A will be .

A= 1 1 1 0 0
1 1 1 0 0
1 1 1 0 0
0 0 0 5 5
0 0 0 5 5
Input

clc
clear all
A=ones(3)
B=[5 5;5 5]
A(4:5,4:5)=B

Output

A=

1 1 1
1 1 1
1 1 1

B=

5 5
5 5

A=

1 1 1 0 0
1 1 1 0 0
1 1 1 0 0
0 0 0 5 5
0 0 0 5 5
Plot related problem

26.program-2D contour plot


Problem: Create a plot from the function
( )√
( ) ( )
over the domain -3<=x<=3 & -3<=y<=3.

Input

clc
clear all
x=-3:.01:3;
y=-3:.01:3;
n=15;
[X,Y]=meshgrid(x,y);
Z=(1.8).^((-1.5)*sqrt(X^2+Y^2)).*(sin(X).*cos(.5*Y));
contour(X,Y,Z,n)
xlabel('x');
ylabel('y');
zlabel('z');

Output

0
y

-1

-2

-3
-3 -2 -1 0 1 2 3
x
27.program-32D contour plot
Problem: Create a plot from the function

( )√
( ) ( )
over the domain -3<=x<=3 & -3<=y<=3.

Input

clc
clear all
x=-3:.25:3;
y=-3:.25:3;
n=15;
[X,Y]=meshgrid(x,y);
Z=(1.8).^((-1.5)*sqrt(X^2+Y^2)).*(sin(X).*cos(.5*Y));
contour3(X,Y,Z,n)
xlabel('x');
ylabel('y');
zlabel('z');

Output

0.5

0
z

-0.5

-1

2
3
2
0 1
0
-2 -1
-2
y -3
x
28.program-3D mesh contour plot
Problem: Create a plot from the function

( )√
( ) ( )
over the domain -3<=x<=3 & -3<=y<=3.

Input

clc
clear all
x=-3:.25:3;
y=-3:.25:3;
n=15;
[X,Y]=meshgrid(x,y);
Z=(1.8).^((-1.5)*sqrt(X^2+Y^2)).*(sin(X).*cos(.5*Y));
meshc(X,Y,Z)
xlabel('x');
ylabel('y');
zlabel('z');

Output

0.5

0
z

-0.5

-1
4
2 4
0 2
0
-2 -2
y -4 -4
x
29.program-3D plot surface
Problem: Create a plot from the function
( )√
( ) ( )
over the domain -3<=x<=3 & -3<=y<=3.

Input

clc
clear all
x=-3:.25:3;
y=-3:.25:3;
n=15;
[X,Y]=meshgrid(x,y);
Z=(1.8).^((-1.5)*sqrt(X^2+Y^2)).*(sin(X).*cos(.5*Y));
surf(X,Y,Z)
xlabel('x');
ylabel('y');
zlabel('z');

Output

0.5

0
z

-0.5

-1
4
2 4
0 2
0
-2 -2
y -4 -4
x
30.program- cylinder plot
Problem: Create a plot from the function
( )√
( ) ( )
over the domain -3<=x<=3 & -3<=y<=3.

Input

clc
clear all
x=-3:.25:3;
y=-3:.25:3;
t=linspace(0,pi,20);
r=1+sin(t);
[X,Y]=meshgrid(x,y);
Z=(1.8).^((-1.5)*sqrt(X^2+Y^2)).*(sin(X).*cos(.5*Y));
[X,Y,Z]=cylinder(r);
surf(X,Y,Z)
axis square
xlabel('x');
ylabel('y');
zlabel('z');

Output

0.8

0.6
z

0.4

0.2

0
2
1 2
0 1
0
-1
-1
y -2 -2
x
31.program- sphere plot
Problem: Create a plot from the function
( )√
( ) ( )
over the domain -3<=x<=3 & -3<=y<=3.

Input

clc
clear all
x=-3:.25:3;
y=-3:.25:3;
n=15;
[X,Y]=meshgrid(x,y);
Z=(1.8).^((-1.5)*sqrt(X^2+Y^2)).*(sin(X).*cos(.5*Y));
[X,Y,Z]=sphere(n)
surf(X,Y,Z)
axis square
xlabel('x');
ylabel('y');
zlabel('z');

Output

0.5

0
z

-0.5

-1
1
0.5 1
0 0.5
0
-0.5
-0.5
y -1 -1
x

You might also like