UCS751_LAB_FILE
UCS751_LAB_FILE
(Deemed to be Univerisity)
Code:-
disp("Hello World")
Output:-
Experiment No. 2
Code:-
sum = a + b;
difference = a - b;
product = a * b;
quotient = a / b;
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
Code:-
Output:-
Experiment No. 5
Code:-
Output:-
Experiment No. 6
Code:-
Output:-
Experiment No. 7
Code:-
// Logical OR operation
result_or = a | b;
disp("a OR b: ", result_or);
Output:-
Experiment No. 8
Code:-
// 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
Code:-
if bitand(n,1) == 0 then
disp("Even number");
else
disp("Odd number");
end
Output:-
Experiment No. 11
Code:-
Output:-
Experiment No. 12
Power function
Code:-
Output:-
Experiment No. 13
Code:-
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
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:-
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);
Output:-
Experiment No. 18
Code:-
// 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
Code:-
x=[0:%pi/16:2*%pi]';
y=sin(x);
plot2d(x,y);
xgrid
xlabel('x');
ylabel('sinx');
Output:-
Experiment No. 20
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
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);
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));
Output:-
Experiment No. 26
Matrices
Code:-
B=A1(3:-1:1,1:4);
disp(B);
disp(eye(2,2));
disp(ones(3,2));
disp(zeros(3,3));
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:-