Lab (6)

talke about loops and if statements some more. Wrote a program for the Monte Carlo computation of PI

% MonteCarlo PI computation

nOfPoints = input('Enter number of points: ');

circleR = input('Enter the radius: ');

countInside = 0;

for i = 1: nOfPoints

x = -circleR + rand()*(2*circleR);

y = -circleR + rand()*(2*circleR);

r = sqrt(x^2 + y^2);

if r < circleR

countInside = countInside + 1;

end

end

ratio = countInside/nOfPoints;

areaOfCircle = ratio*(2*circleR)^2;

myPI = areaOfCircle/circleR^2

Completed the Quadratic Equation Program with simple if ... elseif ... end statements. No composite conditions.

We started improving the Quadratic program by combining some of the conditions to make composite conditions.

Lab (5)

Discuss if end, if elseif end...

We started designing a program for quadratic equation.

We completed the Volume Calculator program that consisted of a main program,

and 3 functions.

We used if ... else if ... else statement.

% The Main program for computing volume for various shapes

%

choice = input('Enter 1-Sphere, 2-Cylindre,3-Cube, other exit ');

if choice == 1

radius = input('Enter the radius of the shpere: ');

volume = sphereVolume(radius);

fprintf('The volume of sphere with radius %d cm is %f cm^3 \n', radius, volu

elseif choice == 2

radius = input('Enter the radius of the cylindre: ');

height = input('Enter the height of the cylindre: ');

volume = cylindreVolume(radius);

fprintf('The volume of cylindre with radius & height % cm is %d cm is %f cm^

elseif choice == 3

length = input('Enter the length of the cube: ');

volume = cubeVolume(length);

fprintf('The voloume of cube with length % cm is %f cm^3 \n', length,volume)

else

fprint('You havent made a valid choice \n');

end

%==================== Main ends here ================

function volume = cubeVolume(length)

% This Function computes the volume of a sphere

% Input: radius in cm

% Output: volumne in cm^3

clc

volume = length ^ 3;

%==================== Sphere ends here ================

function volume = cubeVolume(length)

% This Function computes the volume of a sphere

% Input: radius in cm

% Output: volumne in cm^3

clc

volume = length ^ 3;

%==================== Cube ends here ================

function volume = cylindreVolume(radius, height)

% This Function computes the volume of a sphere

% Input: radius in cm

% Output: volumne in cm^3

clc

volume = height*pi()*radius ^ 2;

%==================== Cylindre ends here ================

Using fprintf and input

if statment, if ... else statement, if .. else if .. else.

We wrote a program that asked the user to enter the radius of a sphere in cm and computed the volume of that sphere in cm^3.

% This program computes the volume of a sphere

% Input: radius in cm

% Output: volumne in cm^3

clc

radiums = input('Enter an integer for radious in cm: ');

volume = (3.0/4)*pi()*radius ^ 3;

fprintf('Valume of sphere with radius %d (cm) is %f (cm^3) \n ', radius, volume);

Then we learned to write a function that instead of asking for the radius would take the radius as its parameetr.

function volume = sphereVolume(radius)

% This Function computes the volume of a sphere

% Input: radius in cm

% Output: volumne in cm^3

clc

volume = (3.0/4)*pi()*radius ^ 3;

fprintf('Valume of sphere with radius %d (cm) is %f (cm^3) \n ', radius, vol

We need to save the as sphereVolume.m and then at the comman prompt can run it

sphereVolume(2)

which takes 2 as the radius and returns the volume.

Started Introduction (1) - Pre-defined functions, variables, different types of variables,

Arrays, index and content in an array

Microlab (1)

Microlab (2)

Lab (2)

Finishing Notes (1)

What is a variable? variable types. Mathematical operations

Introduction, Syllabus, Computer Usage Policy, discussion about the lab

Textbook discussions

Problem Solving Strategies, Microlab 1

Lab (1)