You are on page 1of 37

CHAPTER 2

Sol

Algorithm
1. Make a user based script.
2. Display what script will calculate
3. Display units
4. Put a caution of not entering a vector or a matrix
5. Get the inputs from the user of outer radius and inner radius.
6. Calculate the Volume by the formula given
7. Include the comments in the script .

M file

disp('This software will calculate the volume of a hollow sphere ');

disp('Don,t enter a vector or a matrix');

disp('The units are in inches');

radiusinner=input('Enter a inner radius=');

radiusouter=input('Enter a outer radius=');

volume=(4*3.14)/3*((radiusouter)^3-(radiusinner)^3);
fprintf('The volume of a hollow sphere is = %3.2f\n',volume);

Output

This software will calculate the volume of a hollow sphere

Don,t enter a vector or a matrix

The units are in inches

Enter a inner radius=9

Enter a outer radius=10

The volume of a hollow sphere is = 1134.59

Sol

Algorithm
1. Display what script will calculate.
2. Inputs are given .
3. Put the inputs in the formula
4. Display the output

M file
disp('This software will calculate the molecular weigth of hydrogenperoxide');

oxygen=15.9994;

hydrogen=1.0079;
h2o2=(hydrogen*2)+(oxygen*2);

fprintf('The molecular weigth of hydogenperoxide is %3.4f\n',h2o2);

Output

This software will calculate the molecular weigth of hydrogenperoxide

The molecular weigth of hydogenperoxide is 34.0146

Sol

Algorithm

1. Display the output of script


2. Get the input from the user as a string .
3. Display the length of the string.

M file

disp('This software will show the length of the string');

mystring=input('Please enter a string=','s');

string=length(mystring);

fprintf('The length of the string is %d\n',string)

output
This software will show the length of the string

Please enter a string=fghj

The length of the string is 4


Sol

Algorithm
1. Display script use.
2. Get the input of a real number
3. Show the output upto two decimal places

M file
disp('This software will show the real number upto 2 decimal places');

realnumber=input('Please enter a realnumber=');

fprintf('The realnumber you enter is %0.2f\n',realnumber);

Output
This software will show the real number upto 2 decimal places

Please enter a realnumber=56.986

The realnumber you enter is 56.99


Sol

Algorithm
1. Take the input of a matrix or variable from the user.
2. Show the output using fprintf command.
3. Experiment with different examples.

M file
matrixvector=input('Enter a marix or a vector=');

fprintf('%d',matrixvector); % Do atleast 3 examples

output
Enter a marix or a vector=[1 2 3]

123>>

Enter a marix or a vector=[1 2 3; 4 5 6; 7 8 9]

147258369>>

Enter a marix or a vector=[1 2 3; 4 5 6]

142536>>

Sol
Command window

>> value=12345.6789

value =

1.2346e+004

a)

>> fprintf('without specifying any field width %f\n',value)

without specifying any field width 12345.678900

b)

>> fprintf('in a field width of 10 with four decimal places%10.4f\n',value)

in a field width of 10 with four decimal places12345.6789

c)

>> fprintf('in a field width of 10 with two decimal places%10.2f\n',value)

in a field width of 10 with two decimal places 12345.68

d)

>> fprintf('in a field width of 6 with four decimal places%6.4f\n',value )

in a field width of 6 with four decimal places12345.6789

e)

>> fprintf('in a field width of 2 with 4 decimal places%2.4f',value)

in a field width of 2 with 4 decimal places12345.6789>>


Sol

Command window

a)

>> fprintf('without specifying any field width %d\n',value)

without specifying any field width 12345

b)

>> fprintf('in field width of 5 %5d\n',value)

in field width of 5 12345

c)

>> fprintf('in field width of %8d\n',value)

in field width of 12345

d)

>> fprintf('in field width of 3 %3d\n',value)

in field width of 3 12345


Sol

Command window

>> x=12.34

x=

12.3400

>> y=4.56

y=

4.5600

a)

>> fprintf('x is%7.3f\n',x)

x is 12.340

b)

c)

Sol

Algorithm
1. Display the use of script.
2. Display the units.
3. Get the input from the user of length and width
4. .The area of the rectangle.

M File

disp('This software will calculate the area of rectangle');

disp('The units are in inches');

length=input('Enter a length=');

width=input('Enter a width=');

area=length*width;

fprintf('The area of rectangle is %f\n',area);

Output

This software will calculate the area of rectangle

The units are in inches

Enter a length=4

Enter a width=7

The area of rectangle is 28.000000

Sol
Algorithm
1. Display script use.
2. User enter his or her name.
3. That name will be shown in a sentence format given.

M file
disp('This software will get your name and show in a sentence format given.');

name=input('what is your name=','s');

fprintf('wow,your name is %s\n',name);

Output
This software will get your name and show in a sentence format given.

what is your name=Omer Ishfaque

wow,your name is Omer Ishfaque

Sol

Algorithm
1. Display script use.
2. Get the input of string from user .
3. Display the string

M file
disp('This software will type the string in quotes');

string=input('Enter your string=','s');

fprintf ('Your string was:" %s"\n',string)

Output

This software will type the string in quotes

Enter your string=dsadghjj

Your string was:" dsadghjj"

Sol

Algorithm
1. Display the use of script.
2. Take the input from user in degrees.
3. Make the logic.
4. Display the output in radians

Mfile
disp('This software converts angle from degrees to radians');
degrees=input('Enter the angle in degrees=');

radians=(degrees/pi);

fprintf('The angle in radians is %f\n',radians);

Output
This software converts angle from degrees to radians

Enter the angle in degrees=180

The angle in radians is 57.295780

Sol

Algorithm
1. Display script use.
2. Enter yearly spending and monthly spending.
3. Make the logic.
4. Display the output.

Mfile

disp('This software shows the range of money spending on food');


yearlyspending=input('Please enter your yearly spending=');
monthlyspending=input('Please enter your monthly spending=');

w=8:10; %Percentage range

x=w/100;

y=x*yearlyspending;
z=x*monthlyspending; %logic

disp('The range for yearly spending')

fprintf('%d %d %d \n',y);

disp('The range for monthly spending')

fprintf('%d %d %d \n',z)

Output
This software shows the range of money spending on food

Please enter your yearly spending=10000

Please enter your monthly spending=90000

The range for yearly spending

800 900 1000

The range for monthly spending

7200 8100 9000

Sol

Algorithm
1. Display script use.
2. Take input from the user.
3. Make the logic.
4. Display the output.
5. Show the units.

M file

disp('This software will calculate the wing loading')

weight=input('Enter aircraft weight in kilograms=');

wingarea=input('Enter wing area in meters=');

wingso=(weight/wingarea);

fprintf('The wing loding is %f\n ',wingso);

disp('The units are kg/m^2')

Output

This software will calculate the wing loading


Enter aircraft weight in kilograms=2000
Enter wing area in meters=4
The wing loding is 500.000000
The units are kg/m^2

Sol

Algorithm
1. Display Script use.
2. User don,t enters matrix or a vector.
3. User enters a x and y coordinate.
4. Plot that point on graph.
5. Use green + marking.

M file
disp('This software plots a point on graph');

x=input('Put the x coordinate=');

y=input('Put the y coordinate=');

plot(x,y,'g+')

Output
Sol

Algorithm
1. Display script use.
2. Get x values using colon operator.
3. Plot exp(x).

M file
disp('This script will plot exponential function')

x=-2:0.1:2;

y=exp(x);

plot(x,y,'r*')

xlabel('X')

ylabel('Y')

title('exponential graph')

output
Sol

Algorithm

1. Display script use


2. Put given value of x using colon operaotor.
3. Put value of y using colon operator
4. Plot bar chart.

M file
disp('This script will plot bar chart');

x=1:5:100;

y=sqrt(x);

bar(x,y)

Output

Sol

Algorithm

1. Display script use.


2. Let user enter y and x vectors
3. Use plot function to plot graphs in different styles.
M file
disp('This script will plot graph in different styles')

vec=randint(1,3,[1 ,100]);

x=1:length(vec);

plot(x,vec,'r*')

xlabel('X');

ylabel('Y');

title('graph')

Output
Sol

Algorithm
1. Display script use.
2. Plot 0-10 range.
3. Then Plot 0-100 range.

M file 1

disp('This script plots sinx 0-10 range graph');

x=linspace(0,pi,10);
y=sin(x);

plot(x,y)

Output1

MFile2
disp('This script plots sinx 0-100 range graph');

x=linspace(0,pi,100);

y=sin(x);

plot(x,y)

OUTPUT2
Sol
Sol

Sol

Sol

Sol

Algorithm

1. Display script use.


2. User enters a width and breadth
3. Make the logic
4. Show the area

M file

disp('This software calculates the area of rectangle');

width=input('Enter width=');

length=input('Enter length=');

area=(length*width);

fprintf('The area is %f\n',area)

Output
This software calculates the area of rectangle
Enter width=12
Enter length=21
The area is 252.000000

Sol

Algorithm
1. Display software use.
2. User enters in Fahrenheit.
3. Make the logic
4. Display the output.

M file
disp('This software will convert from fahrenheit to celsius and kelvin');
F=input('Enter value in fahrenheit=');

C=(F-32)*5/9;
K=C+273.15;

fprintf('The value in centigrade is %f and in kelvin is %f\n',C,K);

Output

This software will convert from fahrenheit to celsius and kelvin


Enter value in fahrenheit=100
The value in centigrade is 37.777778 and in kelvin is 310.927778

Sol
Algorithm

1. Display Script use.


2. Enter value in milesper hour.
3. Make the logic.
4. Reply value in meters per second.

Mfile

disp('This script converts from milesperhour to meterspersecond');

mph=input('Enters value in miles per hour=');

mps=(mph*3048)/(3600) ;

fprintf('The value in meterspersecond is %0.3f',mps);

Output

This script converts from milesperhour to meterspersecond


Enters value in miles per hour=19
The value in meterspersecond is 16.087>>

Sol
Algorithm

1. Display software use


2. User enters weigth in pounds and height in inches.
3. Calculate BMI.
4. Show BMI.

M file

disp('This software calculates BMI');

weight=input('Enter weigth in pounds=');

height=input('Enter height in inches=');

BMI=703*((weight)/(height^2));

fprintf('The bmi is %3.2f',BMI);

OUTPUT
This software calculates BMI

Enter weigth in pounds=220

Enter height in inches=81

The bmi is 23.57>>

Sol
Algorithm
1. Display software use
2. User enters total and static pressure
3. Put values in the formula
4. Show velocity

Mfile

disp('This software calculates velocity of moving fluid');

tp=input('Enter total pressure=');

stp=input('Enter static pressure=');

velocity=1.016*(sqrt(tp-stp));

fprintf('The velocity of water is %0.3f\n',velocity);

output

This software calculates velocity of moving fluid


Enter total pressure=1000
Enter static pressure=500
The velocity of water is 22.718

Sol

Algorithm
1. Display script use.
2. User enters the age.
3. Put age in the formula given.
4. Display THR which is output.

M File
disp('This software calculates the target heart beat ');

age=input('Enter your age=');

thr=(220-age)*.6;

fprintf('The target heart beat of a person is %0.3f\n',thr)

Output

This software calculates the target heart beat

Enter your age=30

The target heart beat of a person is 114.000

Sol

Algorithm
1. Display software use
2. User enters n value
3. Make logic
4. Display the output.

M File
disp('This software calculates the factorial using stirling formula');

n=input('Enter the value of n=');

a=sqrt(2*pi)*n*[n/exp(n)]^n; %Logic

fprintf('The factorial is %0.3f\n',a)

Output

This software calculates the factorial using stirling formula


Enter the value of n=8
The factorial is 0.000
This software calculates the factorial using stirling formula
Enter the value of n=98
The factorial is 0.000
This software calculates the factorial using stirling formula
Enter the value of n=98
The factorial is 0.000
This software calculates the factorial using stirling formula
Enter the value of n=8
The factorial is 0.000
This software calculates the factorial using stirling formula
Enter the value of n=98
The factorial is 0.000

Sol
Algorithm

1. Display script use.


2. User enters inches of rain.
3. Convert inches of rain to snow.
4. Display output.

Mfile
disp('This script converts rain to snow in inches');

rain=input('Enter rain in inches=');

snow=rain*6.5;

fprintf('The snow is %0.2f\n',snow);

Output

This script converts rain to snow in inches

Enter rain in inches=1

The snow is 6.50

This script converts rain to snow in inches

Enter rain in inches=77

The snow is 500.50


Sol

Algorithm
1. Display the use of script.
2. Enter a vector.
3. Make the logic.
4. Show the output.

M File
disp('This script generates the random element from the vector');

vec=input('Enter a vector=');

a=rand(1,1,[1,length(vec)]);

fprintf('The random element from vector is %f\n',a)

output
This script generates the random element from the vector

Enter a vector=[9 21 0]

Warning: Input arguments must be scalar.


> In randelemrnent at 6

The random element from vector is 0.913376

Sol

Algorithm
1. Display script use.
2. User enters one value.
3. Make the logic .
4. Display the output.

Mfile

disp('In this script,From one arguement a vector will come in inrement of


five');

arg=input('Enter a single integer arguement=');

vecout=arg:1:(arg+5);

fprintf('%d %d %d %d %d %d',vecout);

output
In this script,From one arguement a vector will come in inrement of five
Enter a single integer arguement=4

4 5 6 7 8 9>>
Sol

Algorithm
1. Display software use
2. User enters two sides and the included angle
3. Make logic
4. Display the output

M file

disp('This software calculates the third side of triangle.');

b=input('Enters first side=');

c=input('Enter second side=');

alpha=input('Enters the angle between them,it is in degrees=');

a=b^2+c^2-(2*b*c)*(cosd(alpha));

side=sqrt(a);

fprintf('The third side of a triangle is %0.3f\n',side);


Output
This software calculates the third side of triangle.
Enters first side=2.2
Enter second side=4.4
Enters the angle between them,it is in degrees=50
The third side of a triangle is 3.429

You might also like