Name of Student: ____________________________
Enrollment No.: ____________________________
Class: ____________________________
Section: ____________________________
Session: ____________________________
Simulation lab [EC-406]
Department of Electronics & Communication Engineering
LAKSHMI NARAIN COLLEGE OF TECHNOLOGY EXCELLENCE
BHOPAL
Kalchuri Nagar, Raisen Road Bhopal (MP) 462023
Answers
Q.1 Write a Scilab code to show the marks of 10 students in a bar graph.
Ans 1. // Marks of 10 students
marks = [80, 75, 90, 85, 70, 65, 95, 85, 75, 88];
// Create a vector for student numbers (1 to 10)
students = 1:10;
// Plot the bar graph
bar(students, marks)
// Add title and labels
title('Marks of 10 Students')
xlabel('Student Number')
ylabel('Marks')
Q.2 Perform basic mathematical operation on Scilab.
Ans 2.
Addition Multiplication
a = 5; a = 5;
b = 3; b = 3;
result = a + b; result = a * b;
disp(result); // Display the result disp(result); // Display the result
Subtraction Division
a = 5; a = 5;
b = 3; b = 3;
result = a - b; result = a / b;
disp(result); // Display the result disp(result); // Display the result
Exponential
a = 5;
b = 3;
result = a^b;
disp(result); // Display the result
Square Root
a = 25;
result = sqrt(a);
disp(result); // Display the result
Trigonometric
angle = %pi / 4; // 45 degrees in
radians
sin_value = sin(angle);
cos_value = cos(angle);
tan_value = tan(angle);
disp(sin_value, cos_value,
tan_value); // Display the results
Q.3 Write a programme to verify various matrix operations.
Ans 3 // Create two sample matrices
A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
B = [9, 8, 7; 6, 5, 4; 3, 2, 1];
disp('Matrix A:');
disp(A);
disp('Matrix B:');
disp(B);
// Matrix addition
disp('Matrix Addition (A + B):');
disp(A + B);
// Matrix subtraction
disp('Matrix Subtraction (A - B):');
disp(A - B);
// Matrix multiplication
disp('Matrix Multiplication (A * B):');
disp(A * B);
// Matrix transpose
disp('Transpose of Matrix A:');
disp(A');
// Matrix inverse
disp('Inverse of Matrix A:');
disp(inv(A));
// Determinant of Matrix A
disp('Determinant of Matrix A:');
disp(det(A));
// Element-wise multiplication
disp('Element-wise Multiplication (A .* B):');
disp(A .* B);
// Element-wise division
disp('Element-wise Division (A ./ B):');
disp(A ./ B);
// Matrix exponentiation
disp('Matrix Exponentiation (A^2):');
disp(A^2);
// Matrix power (element-wise exponentiation)
disp('Matrix Power (A .^ 2):');
disp(A .^ 2);
Q.4 Write a Scilab Code to show 2D Output.
Ans 2 Below is a simple example code that plots a sine wave:
// Define the x-axis values
x = linspace(0, 2*%pi, 100); // Generate 100 points from 0 to 2*pi
// Calculate the y-axis values (sine of x)
y = sin(x);
// Plot the sine wave
plot2d(x, y, style = 1, rect = [0, -1, 2*%pi, 1]); // style = 1 for lines, rect = [xmin, ymin, xmax,
ymax] for setting the axis limits
// Add labels and title
xlabel('x');
ylabel('sin(x)');
title('Sine Wave');
// Add grid
grid on;
// Add a legend
legend('sin(x)');
Q.5 To plot graph of two functions.
Ans 5 // Define the range of x values
x = -10:0.1:10;
// Define the first function, for example, f(x) = x^2
y1 = x.^2;
// Define the second function, for example, g(x) = sin(x)
y2 = sin(x);
// Plot the first function
plot(x, y1, color='blue', label='f(x) = x^2');
// Add the second function to the same plot
plot(x, y2, color='red', label='g(x) = sin(x)');
// Add title and labels to the plot
title('Plot of f(x) = x^2 and g(x) = sin(x)');
xlabel('x');
ylabel('y');
legend('topright');
// Display the plot
xgrid();
Q.6 Write for and while loop codes to perform given functions using Scilab edit window.
Ans 6 (i) Using for loop to compute the sum of elements in an array:-
function sum = array_sum(arr)
sum = 0;
for i = 1:length(arr)
sum = sum + arr(i);
end
endfunction
// Example usage
arr = [1, 2, 3, 4, 5];
result = array_sum(arr);
disp(result);
(ii) Using while loop to find factorial of a number:-
function fact = factorial(n)
if n == 0 || n == 1
fact = 1;
else
fact = 1;
i = 1;
while i <= n
fact = fact * i;
i = i + 1;
end
end
endfunction
// Example usage
n = 5;
result = factorial(n);
disp(result);
Q.7 Plot sine and cosine graphs using Scilab edit window.
Ans 7 // Define the range of x values
x = -2*%pi:0.1:2*%pi;
// Calculate sine and cosine values
y_sin = sin(x);
y_cos = cos(x);
// Plot the sine function
plot(x, y_sin, color='blue', label='sin(x)');
// Add the cosine function to the same plot
plot(x, y_cos, color='red', label='cos(x)');
// Add title and labels to the plot
title('Plot of sin(x) and cos(x)');
xlabel('x');
ylabel('y');
legend('topright');
// Display the plot
xgrid();
Q.8 Generate discrete signal, exponential signal and ramp signal.
Ans 8 In Scilab, you can generate discrete signals, exponential signals, and ramp signals using simple
code.
(i) Generating Discrete Signal
// Define the time vector
t = 0:0.1:10; // Time points from 0 to 10 with a step of 0.1
// Define the discrete signal (e.g., sine wave)
discrete_signal = sin(t);
// Plot the discrete signal
plot(t, discrete_signal);
xlabel('Time');
ylabel('Amplitude');
title('Discrete Signal');
(ii) Generating Exponential Signal
// Define the time vector
t = 0:0.1:10; // Time points from 0 to 10 with a step of 0.1
// Define the exponential signal
exponential_signal = exp(t);
// Plot the exponential signal
plot(t, exponential_signal);
xlabel('Time');
ylabel('Amplitude');
title('Exponential Signal');
(iii) Generating Ramp Signal
// Define the time vector
t = 0:0.1:10; // Time points from 0 to 10 with a step of 0.1
// Define the ramp signal
ramp_signal = t;
// Plot the ramp signal
plot(t, ramp_signal);
xlabel('Time');
ylabel('Amplitude');
title('Ramp Signal');
Q.9 Study different types of graphs.
Ans 9 In Scilab, you can create various types of graphs to visualize your data. Some common
types of graphs include:
1. Line plot
2. Scatter plot
3. Bar plot
4. Histogram
5. Pie chart
6. Contour plot
7. Surface plot
8. Vector field plot
9. Stem plot
10. Polar plot
Here are examples of how to create these types of graphs in Scilab:
1. Line Plot
x = 0:0.1:2*%pi;
y = sin(x);
plot(x, y);
2. Scatter plot
x = rand(1, 50);
y = rand(1, 50);
scatter(x, y);
3. Bar plot
x = 1:10;
y = rand(1, 10);
bar(x, y);
4. Histogram
data = rand(1, 100);
histplot(data);
5. Pie chart
data = [30, 20, 50];
pie(data);
6. Contour plot
x = linspace(-2, 2, 100);
y = linspace(-2, 2, 100);
[X, Y] = meshgrid(x, y);
Z = X.^2 + Y.^2;
contour(X, Y, Z);
7. Surface plot
x = linspace(-2, 2, 100);
y = linspace(-2, 2, 100);
[X, Y] = meshgrid(x, y);
Z = X.^2 + Y.^2;
surf(X, Y, Z);
8. Vector field plot
x = linspace(-2, 2, 10);
y = linspace(-2, 2, 10);
[X, Y] = meshgrid(x, y);
U = -Y;
V = X;
quiver(X, Y, U, V);
9. Stem plot
x = 0:10;
y = rand(1, 11);
stem(x, y);
10. Polar plot
theta = linspace(0, 2*%pi, 100);
r = sin(2*theta);
polarplot(theta, r);
Q.10 Solve linear equations using matrix on Scilab.
Ans 10 An example of how you can solve a system of linear equations using matrices in Scilab.
// Define the coefficients matrix A
A = [2, 1, -1;
-3, -1, 2;
-2, 1, 2];
// Define the constants vector b
b = [8; -11; -3];
// Solve the system of equations Ax = b
x = A \ b;
// Display the solution vector x
disp(x);
In this example, A is the coefficient matrix of the system of linear equations, and b is the constants
vector. We then solve the system by using the backslash operator \ to find the solution vector x.
Finally, we display the solution vector x.