1. Discuss machine code.
Machine code is a low-level programming language consisting of binary instructions that a computer's CPU can execute directly. Each instruction performs a very specific task, such as arithmetic operations or memory access, and is unique to the architecture of the CPU.
2. How do you use variables in MATLAB?
In MATLAB, variables are created by simply assigning a value to a name. For example:
x = 5; % Assigns the value 5 to the variable xy = x + 3; % Uses the value of x to assign a new value to yz = 'Hello'; % Assigns a string to the variable z (code-box)
3. How do you use arguments and return values in MATLAB?
Functions in MATLAB can take input arguments and return output values. For example:
function result = add(a, b)result = a + b;end% Calling the functionsum = add(3, 4); % sum will be 7 (code-box)
4. What are control statements?
Control statements in MATLAB manage the flow of execution. Common control statements include:
if-else: Conditional execution.
for: Looping a specific number of times.
while: Looping while a condition is true.
switch: Multi-way branching based on the value of an expression.
5. What are M-files?
M-files are files that contain MATLAB code and have a .m extension. They can be either scripts or functions. Scripts run a series of commands, while function M-files define reusable functions with inputs and outputs.
6. What is randomising a list?
Randomising a list means rearranging its elements in a random order. In MATLAB, this can be done using the '
randperm' function. For example:list = [1, 2, 3, 4, 5];randomList = list(randperm(length(list)));% randomList will be a random permutation of [1, 2, 3, 4, 5] (code-box)
7. WAP in MATLAB to find the sum of first 'n' even natural numbers.
% Define the number of termsn = 10;% Initialize the sumsum_even = 0;% Loop through the first 'n' even natural numbersfor k = 1:nsum_even = sum_even + 2*k;end% Display the resultdisp(['The sum of the first ', num2str(n), ' even natural numbers is: ', num2str(sum_even)]); (code-box)
This code calculates the sum of the first
n even natural numbers by iterating through them and summing them up.8. WAP in MATLAB to count the number of characters in a string.
% Define the stringstr = 'Hello, MATLAB!';% Count the number of charactersnum_chars = length(str);% Display the resultdisp(['The number of characters in the string is: ', num2str(num_chars)]); (code-box)
This code uses the
length function to count the number of characters in the string str.9. Discuss graph plotting in MATLAB.
MATLAB provides powerful tools for plotting graphs. Here are the basic steps and functions:
- Creating a Plot:
x = 0:0.1:10; % Define the x-axis valuesy = sin(x); % Define the y-axis valuesplot(x, y); % Plot the graph (code-box)
- Adding Labels and Titles:
xlabel('X-axis');ylabel('Y-axis');title('Sine Wave'); (code-box)
- Customizing the Plot:
plot(x, y, 'r--'); % Red dashed linegrid on; % Add grid lines (code-box)
- Multiple Plots in One Figure:
y1 = sin(x);y2 = cos(x);plot(x, y1, 'b', x, y2, 'r');legend('Sine', 'Cosine'); (code-box)
These commands help in creating, labeling, and customizing plots in MATLAB.
11. WAP in MATLAB to arrange some integers in ascending order using bubble sort.
% Define the array of integersarr = [5, 2, 9, 1, 5, 6];% Get the length of the arrayn = length(arr);% Bubble Sort algorithmfor i = 1:n-1for j = 1:n-iif arr(j) > arr(j+1)% Swap the elementstemp = arr(j);arr(j) = arr(j+1);arr(j+1) = temp;endendend% Display the sorted arraydisp('The sorted array in ascending order is:');disp(arr); (code-box)
12. Discuss different string handling functions available in MATLAB.
MATLAB provides various functions for string handling, including:
strcat: Concatenates strings.
str1 = 'Hello';str2 = 'World';result = strcat(str1, ' ', str2); % 'Hello World' (code-box)
strfind: Finds occurrences of a substring.
str = 'Hello World';idx = strfind(str, 'World'); % Returns the starting index of 'World' (code-box)
strcmp: Compares two strings for equality.
isEqual = strcmp('MATLAB', 'MATLAB'); % Returns 1 (true) (code-box)
strrep: Replaces occurrences of a substring.
str = 'Hello World';newStr = strrep(str, 'World', 'MATLAB'); % 'Hello MATLAB' (code-box)
lowerandupper: Converts strings to lower or upper case.
str = 'Hello World';lowerStr = lower(str); % 'hello world'upperStr = upper(str); % 'HELLO WORLD' (code-box)
sprintf: Formats data into a string.
num = 10;str = sprintf('The number is %d', num); % 'The number is 10' (code-box)
These functions allow for a wide range of string manipulations in MATLAB.
13. WAP in MATLAB to multiply two matrix of order 4x4.
% Define two 4x4 matricesA = [1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12; 13, 14, 15, 16];B = [16, 15, 14, 13; 12, 11, 10, 9; 8, 7, 6, 5; 4, 3, 2, 1];% Multiply the matricesC = A * B;% Display the resultdisp('The product of matrices A and B is:');disp(C); (code-box)
14. WAP in MATLAB to find the roots of the polynomial p(x) = x^4 + 8x^3 + 25x^2 - 40x + 30
% Define the polynomial coefficientsp = [1 8 25 -40 30];% Find the roots of the polynomialroots_p = roots(p);% Display the rootsdisp('The roots of the polynomial p(x) = x^4 + 8x^3 + 25x^2 - 40x + 30 are:');disp(roots_p); (code-box)
15. WAP in MATLAB to create a script file which will call two functions circle and rectangle where circle calculates the area of circle and draw the circle and rectangle calculates the area of rectangle and draw the same. The inputs are radius of circle and length and width of rectangle.
Script File (main.m):
% Input radius of circleradius = 5;% Input length and width of rectanglelength = 10;width = 4;% Call the circle function[area_circle, h_circle] = circle(radius);disp(['Area of the circle: ', num2str(area_circle)]);% Call the rectangle function[area_rectangle, h_rectangle] = rectangle(length, width);disp(['Area of the rectangle: ', num2str(area_rectangle)]); (code-box)
Function File for Circle (circle.m):
function [area, h] = circle(radius)% Calculate the area of the circlearea = pi * radius^2;% Draw the circletheta = linspace(0, 2*pi, 100);x = radius * cos(theta);y = radius * sin(theta);h = figure;plot(x, y);axis equal;title('Circle');xlabel('X-axis');ylabel('Y-axis');end (code-box)
Function File for Rectangle (rectangle.m):
function [area, h] = rectangle(length, width)% Calculate the area of the rectanglearea = length * width;% Draw the rectanglex = [0, length, length, 0, 0];y = [0, 0, width, width, 0];h = figure;plot(x, y);axis equal;title('Rectangle');xlabel('X-axis');ylabel('Y-axis');end (code-box)
16. Discuss different types of control statements used in MATLAB with the help of examples.
MATLAB provides several control statements to manage the flow of execution:
if-else Statement:% Example of if-else statementx = 5;if x > 0disp('x is positive');elseif x < 0disp('x is negative');elsedisp('x is zero');end (code-box)
for Loop:% Example of for loopfor i = 1:5disp(['Iteration: ', num2str(i)]);endwhileLoop:% Example of while loopi = 1;while i <= 5disp(['Iteration: ', num2str(i)]);i = i + 1;end (code-box)
switch Statement:% Example of switch statementx = 3;switch xcase 1disp('x is 1');case 2disp('x is 2');case 3disp('x is 3');otherwisedisp('x is something else');end (code-box)
.jpg)
