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

UCS751_LAB_FILE

Experiments of simulation and modelling course

Uploaded by

Avantika Raina
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

UCS751_LAB_FILE

Experiments of simulation and modelling course

Uploaded by

Avantika Raina
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

UCS751- SIMULATION AND MODELING

2024-2025 ODD SEMESTER

Name:- Avantika Raina Group: 4CO2/4C12 Roll no: 102283002

Submitted to: Dr Sanjeev Rao

Thapar Institute of Engineering and Technology

(Deemed to be Univerisity)

Patiala, Punjab (147001)


TABLE OF CONTENTS
Experiment No. 1

Print Hello World

Code:-

disp("Hello World")

Output:-
Experiment No. 2

Arithmetic Operations (Addition, Subtraction, Division, Multiplication)

Code:-

a=input("Enter value of the first variable: ")


b=input("Enter value of the second variable: ")

sum = a + b;
difference = a - b;
product = a * b;
quotient = a / b;

// Display the results using string concatenation


disp("Sum: " + string(sum));
disp("Difference: " + string(difference));
disp("Product: " + string(product));
disp("Quotient: " + string(quotient));

Output:-
Experiment No. 3

Variable Assignment

Code:-

a=10.2;
b=5;

name="Avantika";
greeting="Hello, World!";

complex_number=3 + 4*%i;

is_true=%T;
is_false=%F;

pi=3.14;
radius=5;
area=pi*radius*radius;
disp("Area of circle having radius 5 is",string(area));

array=[1,2,3,4,5,6];
matrix=[1,2,3;4,5,6];

Output:-
Experiment No. 4

Square root of a number

Code:-

r=input("Enter any real number :")

disp("Square root of the number is",string(sqrt(r)));

Output:-
Experiment No. 5

Area of circle (input radius)

Code:-

radius=input("Input the radius of the circle ");

disp("Area of the circle is",string(%pi*radius*radius));

Output:-
Experiment No. 6

Greater of 2 numbers (Relational operators)

Code:-

//Get input from the user


a = input("Enter value of first number: (a)");
b = input("Enter value of second number (b)");

// Check if a is greater than b and convert numbers to strings for concatenation


if a > b then
disp(string(a) + " is greater than " + string(b));
elseif a < b then
disp(string(b) + " is greater than " + string(a));
else
disp(string(a) + " and " + string(b) + " are equal");
end

Output:-
Experiment No. 7

Logical AND, OR, NOT

Code:-

// Assign boolean values to variables


a = %T;
b = %F;

// Logical AND operation


result_and = a & b;
disp("a AND b: ", result_and);

// Logical OR operation
result_or = a | b;
disp("a OR b: ", result_or);

// Logical NOT operation


result_not_a = ~a;
disp("NOT a: ", result_not_a);

Output:-
Experiment No. 8

Bitwise AND, OR, XOR, SHIFT

Code:-

// Assign values to variables


a = 10;
b = 5;

// Bitwise AND
result_and = bitand(a, b);
disp("Bitwise AND: ", result_and);

// Bitwise OR
result_or = bitor(a, b);
disp("Bitwise OR: ", result_or);

// Bitwise XOR
result_xor = bitxor(a, b);
disp("Bitwise XOR: ", result_xor);

// Bitwise Complement
result_not_a = bitcmp(a);
disp("Bitwise NOT of a: ", result_not_a);

// Left Shift
function result=leftshift(x, n)
result=x* (2^n);
endfunction
result_left_shift = leftshift(a,1);
disp("Left Shift of a by 1: ", result_left_shift);

// Right Shift
function result=rightshift(x, n)
result = x/(2^n);
endfunction
result_right_shift = rightshift(a,1);
disp("Right Shift of a by 1: ", result_right_shift);
Output:-
Experiment No. 9

Set operations

Code:-

A = [1 2 3 4];
B = [3 4 5 6];

C=union(A,B);
D=intersect(A,B);
E=setdiff(A,B);

F=[1 2 3 4 3 4 5 6];
G=unique(F);

disp(C,D,E,G);

Output:-
Experiment No. 10

WAP to check whether a number is odd or even

Code:-

n=input("Enter the number you want to check for even or odd:");

if bitand(n,1) == 0 then
disp("Even number");
else
disp("Odd number");
end

Output:-
Experiment No. 11

Find greatest among 3 numbers

Code:-

// Get input from the user


a = input("Enter the first number: ");
b = input("Enter the second number: ");
c = input("Enter the third number: ");

// Find the greatest number using nested if-else


if a > b then
if a > c then
disp(string(a) + " is the greatest number.");
else
disp(string(c) + " is the greatest number.");
end
else
if b > c then
disp(string(b) + " is the greatest number.");
else
disp(string(c) + " is the greatest number.");
end
end

Output:-
Experiment No. 12

Power function

Code:-

// Get the base and exponent from the user


base = input("Enter the base: ");
exponent = input("Enter the exponent: ");

// Calculate the power using the pow function


result = (base^exponent);

// Display the result


disp("The result is: " + string(result));

Output:-
Experiment No. 13

Find roots of a quadratic equation

Code:-

// Get coefficients from the user


a = input("Enter the coefficient of x^2: ");
b = input("Enter the coefficient of x: ");
c = input("Enter the constant term: ");

// Form the polynomial


p = poly([a b c],'x');

// Find the roots of the polynomial


roots = roots(p);

// Display the roots


disp("The roots of the quadratic equation are: ");
disp(roots);

Output:-
Experiment No. 14

Nested function

Code:-

// Outer function
function result=outer_function(x)
// Inner function
function y=inner_function(a)
y = a^2 + x;
endfunction

result = inner_function(x) + 5;
endfunction

// Call the outer function


x = 3;
output = outer_function(x);
disp(output);

Output:-
Experiment No. 15

Private function

Code:-

function result=publicFunction(x)
result=_privateFunction(x);
endfunction

function res=_privateFunction(x)
res=x+42;
endfunction

result=publicFunction(10);

disp(result);

Output:-
Experiment No. 16

Private function

Code:-

// Declare a global variable


global x;

// Assign a value to the global variable


x = 10;

// Define a function that accesses and modifies the global variable


function my_function()
global x;
x = x + 5;
disp(x);
endfunction

// Call the function


my_function();

// Display the global variable's value


disp(x);

Output:-
Experiment No. 17

Strings

Code-

//ascii- converts a string into a vector of ascii codes and vice versa
disp(ascii(['hello';'world']))
disp(ascii("scilab"));
disp(ascii([115 99 105 108 97 98]));
//blanks(n)- a string of n blanks
disp(['xxx' blanks(20) 'yyy'])
//char- converts matrix to corresponding ascii codes
x=matrix(61:84, [4,2,3]);
y=char(x)
size(x);
size(y);
disp(y);

//convstr converts to upper or lower case


A=['this','is';'my','matrix'];
convstr(A,'u')
//The emptystr function returns a matrix of zero length character strings.
x=emptystr();
for k=1:10
x = x + ',' + string(k)
emptystr(1,k) + string(k)
end
//isalphanum- checks for alphanumerics
s='A1,B2,C3';
disp(isalphanum(s));
//isascii- checks if each character is ascii or not
disp(isascii("Señor"));
//justify justifies the strings in a vector or matrix
m = ["a" "bcdef" "ghi" ; "jklm" "" "n" ; "opq" "rs" "tuvwxy"]
justify(m)
//part extracts character from a string
part("How to use ""part"" ?", 8:11);
//strcat - concatenates strings
disp(strcat(["a","b"]));
//strchr- first occurence of a character in string
disp(strchr('This is a sample string','s'));
//strcmp compares strings
disp(strcmp('Scilab','scilab'));
//strcspn- lengths from the beginning of strings until a character among those prohibited
disp(strcspn('fbdn12345','123456789'));
//strindex returns index of a character instring
disp(strindex('SCI/demos/scicos','/'));
//stripblanks- strips the gaps in string from left and right both
disp(stripblanks(' 123 '));
//strrev reverse the order of string
disp(strrev("scilab"));
//strsplit spit characters of string
disp(strsplit('scilab'));
//strsubst replaces the string with another specified string
disp(strsubst('SCI/demos/scicos','SCI','.'));

Output:-
Experiment No. 18

Data type conversion

Code:-

// Data type conversions

// Numeric to string
num = 123.45;
str_num = string(num);
disp("Number as string:", str_num);

// String to number
str_num = "456.78";
num = strtod(str_num);
disp("String as number:", num);

// Integer to boolean
int_val = 0;
bool_val = int_val ~= 0;
disp("Integer to boolean:", bool_val);

// Boolean to integer
bool_val = %T;
int_val = bool_val;
disp("Boolean to integer:", int_val);

// Matrix to vector
matrix = [1 2 3; 4 5 6];
vector = matrix(:);
disp("Matrix to vector:", vector);

// Vector to matrix
vector = [1 2 3 4 5 6];
matrix = [vector(1:3); vector(4:6)];
disp("Vector to matrix:", matrix);
Output:-
Experiment No. 19

Plot graph with title and labels

Code:-

x=[0:%pi/16:2*%pi]';
y=sin(x);
plot2d(x,y);
xgrid
xlabel('x');
ylabel('sinx');

Output:-
Experiment No. 20

Drawing multiple functions on the same graph

Code:-

x=[0:%pi/16:2*%pi]';
y=[cos(x) sin(x)];
plot2d(x,y);
xgrid
xlabel('x');
ylabel('cosx + sinx');

Output:-
Experiment No. 21

Subplots

Code:-

subplot(221)
plot2d()

subplot(222)
plot3d()

subplot(2,2,3)
param3d()

subplot(2,2,4)
bar3d()

Output:-
Experiment No. 22

Bar charts

Code:-

scf(0);
y=[1 -3 5];
bar(y,0.5,'yellow');

scf(1);
x1=[1 2 5];
y1=[1 -5 6;3 -2 7;4 -3 8];
bar(x1,y1);

scf(2);
x2=[1 2 5];
y2=[1 4 7;2 5 8;3 6 9];
bar(x2,y2,'stacked');

scf(3);
x3=[1 2 5];
y3=[1 4 7;2 5 8;3 6 9];
bar(x3,y3,0.2,'green');

Output:-
Experiment No. 23

Contours

Code:-

t=linspace(-%pi,%pi,30);
function z=my_surface(x, y),z=x*sin(x)^2*cos(y),endfunction;
contour(t,t,my_surface,10);

Output:-
Experiment No. 24

Three dimensional plot

Code:-

t=[0:0.3:2*%pi]';
z=sin(t)*cos(t');
plot3d(t,t,z);

Output:-
Experiment No. 25

Arrays

Code:-

a=[1 2 3 4 5 6 7 8 9 10];
a1=[1;2;3;4;5;6;7;8;9;10];
a2=1:10;
a3=100:-10:0;
disp(a);
disp(a1);
disp(a2);
disp(a3);

a4=[2+3*%i, 4+1*%i, 3, 5, 6];


b=[1+6*%i, 4+6*%i, 3, 4, 6];

c=a4+b;
disp(c);
disp(a4-b);
//disp(a2*a3);

a5=linspace(0,10,5);
a6=logspace(0,4,3);
disp(a6);
a7=[1 10 25 50 15];
disp(a7(3));

disp(sum(a7));
disp(mean(a7));
disp(length(a7));
disp(max(a7));

disp(min(a7));
disp(prod(a7));
disp(sign(a7));
disp(find(a7));

p=[1.4 10.7 -1.1 20.9];


disp(fix(p));
disp(floor(p));
disp(ceil(p));
disp(round(p));
disp(gsort(p));

Output:-
Experiment No. 26

Matrices

Code:-

A=[16 3 2 13;5 10 11 8;9 6 7 12;4 15 14 1];


disp(sum(A));
disp(sum(A,'c'));
disp(sum(A,'r'));

A1=[3 11 6 5;4 7 10 2;13 9 0 8];


disp(A1(2,3));
disp(A1(:,2));
disp(A1(2,:));
disp(A1(9));

B=A1(3:-1:1,1:4);
disp(B);
disp(eye(2,2));
disp(ones(3,2));
disp(zeros(3,3));

C=[[1 2;3 4],[2 3; 5 6]];


disp(rand(2,3));
A2=[1 2 3; 4 5 6;7 8 9];
disp(diag(A2));
disp(diag(A2,-1));

A3=[1 2;0 4];


disp(det(A3));
disp(rank(A3));
disp(trace(A3));
disp(inv(A3));
disp(A3');
disp(norm(A3));
disp(poly(A3,'x'));
disp(spec(A3));
Output:-
Experiment No. 27

Generate Random Numbers

Code:-

r=rand()
disp(r);
// Get one 4-by-6 matrix of doubles (based on the current distribution)
r=rand(4,6)
disp(r);
// Get one 4-by-6 matrix of doubles with uniform entries
r=rand(4,6,"uniform")
disp(r);
// Produce a matrix of random doubles with the same size as x
x=rand(4,4);
disp(x);
r=rand(x,"normal")
// Produce a 2-by-2-by-2 array of random doubles
r=rand(2,2,2)
disp(r);
// Set the rand generator to normal
rand("normal")
r=rand(4,6)
disp(r);
// Get the current distribution
key=rand("info")
// Set the rand generator to uniform
rand("uniform")
r=rand(4,6)
key=rand("info")
// Produce a 2-by-3 matrix of random complex doubles
x=rand(2,3)+%i*rand(2,3)
// Produce a matrix of random complex doubles with
// normal entries and the same size as x
r=rand(x,"normal")
disp(r);
Output:-

You might also like