0% found this document useful (0 votes)
95 views10 pages

PSLP File 1

The document discusses installing and using Scilab to demonstrate programming concepts like matrix multiplication, loops, conditional statements, and plotting. It provides code examples for multiplying matrices by scalars and other matrices, using loops and conditional statements to iterate through values and check for even/odd numbers, and plotting the square function. A second program demonstrates simulating coin flips to show the theoretical probability limit is approached as the number of flips increases.

Uploaded by

Manan Anand
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)
95 views10 pages

PSLP File 1

The document discusses installing and using Scilab to demonstrate programming concepts like matrix multiplication, loops, conditional statements, and plotting. It provides code examples for multiplying matrices by scalars and other matrices, using loops and conditional statements to iterate through values and check for even/odd numbers, and plotting the square function. A second program demonstrates simulating coin flips to show the theoretical probability limit is approached as the number of flips increases.

Uploaded by

Manan Anand
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/ 10

Program – 1

Objective: Installation of Scilab and demonstration of simple programming concepts like


matrix multiplication (scalar and vector), loop, conditional statements and plotting

Formulation and Method :

Installation of SCILAB:

Scilab is numerical computation software that anybody can freely download.

1. Scilab can be downloaded at the following address:

http://www.scilab.org/.

2. Wait for the installation to be Complete.

3. Open the file Installed.

4. Allow the Changes and accept license and click next.

5. Finish When done and start Programming.

Making directory by name in the root folder and changing the current

directory .

1. Click on the Main Menu

2. Menu File

3. Change current directory visually using a dialog box.

4. Publish the changes


Simple arithmetic on command prompt:
i. Multiplication of a matrix with a scalar.

clc
m = input('number of rows: ');
n = input('number of columns: ');
a = zeros(m,n);
for i=1:m
for j=1:n
a(i,j)=input(sprintf('enter a(%d,%d): ',i,j));
end
num=input('Enter the Scalar Number: ');
res=a*num;
disp("REQUIRED OUTPUT :")
disp(res);

Result :
ii. Multiplication of a matrix with a matrix

clc
disp("Enter matrix 1");
m = input('number of rows: ');
n=
input('n
umber
of
columns
: ');
mat1 =
zeros(m
,n); for
i=1:m
for j=1:n
mat1(i,j)=input(sprintf('enter a(%d,%d): ',i,j));
end
end
disp("Enter matrix 2");
x = input('number of rows: ');
y = input('number of columns: ');
mat2 = zeros(x,y);
for i=1:x
for j=1:y
mat2(i,j)=input(sprintf('enter a(%d,%d): ',i,j));
end
end
res=mat1*mat2;
disp(res);
Result:

i. Loop 100 times and print if the number is odd or even.


clc
for i=1:100
if modulo(i,2)==0 then
disp(i," is Even");
else
disp(i," is Odd");
end
end
clc;
i=0;
while i<=100
if modulo(i,2)==0 then
disp(i," is Even");
else
disp(i," is Odd");
end
i=i+1;
end
Result :

i. Plot (2D) the square function (squares of 1 to 10).

clc;
x=1:10;
y=x.*x;
plot(x,y);
xlabel("Natural number");
ylabel("Sqaure of number");
title("Square Plot");
Result :
Program – 2
Objective : Program for demonstration of Theoretical Probability Limit .

Formulation and Method :

The demonstration is done by simulating the flipping of a coin. The objective is to show
that as the Number of flips increases, the theoretical probability limit of 0.5 is
approached. The practical Probability is simulated by the generation of the random
number for each flip as follows:

a. Generate a random number say toss between 0 and 1.

b. Round the Number obtained and consider x=random variable, and x=Numbers of Head
obtained in the experiment.

c. Repeat this experiment for the various iterations up to 1, 00,000 and keep storing the
result in an Array. Calculate the probability in each case.

d. Plot the graph between the Number of the throw and the Probability.

Result:

function probabilities = flip_coins(num_flips)


results = [];
heads_count = 0;
for i = 1:num_flips
toss = rand();
if toss < 0.5 then
heads_count = heads_count + 1;
end
results(i) = heads_count / i;
end
probabilities = results;
endfunction

num_iterations = 100000;
probabilities = flip_coins(num_iterations);

x = 1:num_iterations;
plot(x, probabilities);
xlabel('Number of Flips');
ylabel('Probability of Heads');
title('Demonstration of Theoretical Probability Limit .');

Graph:

You might also like