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/ 7
Numerical Analysis and Computations-LAB
DEPARTMENT OF MECHATRONICS AND BIOMEDICAL ENGINEERING
NAME: Muhammad Saud Dar
Muhammad Bilal Abdul Rehman
ROLL NO: 212177
211180 211247
CLASS: BEMTS-F21-B
DATE: 17-10-2024
LAB INSTRUCTOR: Ma’am Arisha Ali
Assignment 1 Question No. 1 Write a script that performs basic arithmetic operations. • Ask the user to input two numbers. • Calculate and display the sum, difference, product, and quotient of the two numbers.
% Basic Arithmetic Operations Script
% Ask the user to input two numbers
num1 = input('Enter the first number: '); num2 = input('Enter the second number: ');
% Calculate the sum, difference, product, and quotient
if num2 ~= 0 quotient_result = num1 / num2; else quotient_result = 'undefined (division by zero)'; end
% Display the results
fprintf('The sum of the two numbers is: %f\n', sum_result); fprintf('The difference of the two numbers is: %f\n', difference_result); fprintf('The product of the two numbers is: %f\n', product_result); if isnumeric(quotient_result) fprintf('The quotient of the two numbers is: %f\n', quotient_result); else fprintf('The quotient of the two numbers is: %s\n', quotient_result); end Question No. 2 Use a for loop to calculate the factorial of a number. • Prompt the user to input a non-negative integer. • Calculate the factorial using a for loop. • Display the result.
% Factorial Calculation Script
% Prompt the user to input a non-negative integer
num = input('Enter a non-negative integer: ');
% Check if the input is valid (non-negative integer)
if num < 0 || floor(num) ~= num fprintf('Invalid input. Please enter a non-negative integer.\n'); else % Initialize the factorial result factorial_result = 1;
% Calculate the factorial using a for loop
for i = 1:num factorial_result = factorial_result * i; end
% Display the result
fprintf('The factorial of %d is: %d\n', num, factorial_result); end Question No. 3 Write a program to determine if a number is even or odd. • Prompt the user to enter an integer. • Use an if statement to check if the number is even or odd and display an appropriate message.
% Even or Odd Determination Program
% Prompt the user to enter an integer
num = input('Enter an integer: ');
% Use an if statement to check if the number is even or odd
if mod(num, 2) == 0 fprintf('The number %d is even.\n', num); else fprintf('The number %d is odd.\n', num); end Question No. 4 Create a simple plot of a mathematical function. • Define a function f(x) = x^2 • Generate values of x from −10 to 10. • Use MATLAB to plot f(x) and label the axes and title.
% Simple Plot of f(x) = x^2
% Define the range of x from -10 to 10
x = -10:0.1:10;
% Define the function f(x) = x^2
f_x = x.^2;
% Plot the function
figure; plot(x, f_x);
% Label the axes
xlabel('x'); ylabel('f(x) = x^2');
% Add a title title('Plot of f(x) = x^2');
% Display grid lines for better visualization
grid on; Question No. 5 Write a function that calculates the area of a rectangle. • Define a function area_rectangle(length, width) that returns the area. • Prompt the user for the length and width, call the function, and display the result.
% Main script
% Prompt the user for the length and width
length = input('Enter the length of the rectangle: '); width = input('Enter the width of the rectangle: ');
% Call the function to calculate the area
area = area_rectangle(length, width);
% Display the result
fprintf('The area of the rectangle is: %.2f\n', area);
% Function to calculate the area of a rectangle
function area = area_rectangle(length, width) area = length * width; end Question No. 6 Perform basic operations on vectors. • Create two vectors of the same length (e.g., A = [1, 2, 3] and B = [4, 5, 6]). • Calculate and display the following: o The sum of the vectors. o The element-wise product of the vectors. o The dot product of the vectors.
% Define two vectors of the same length
A = [1, 2, 3]; B = [4, 5, 6];
% Calculate the sum of the vectors
sum_vectors = A + B;
% Calculate the element-wise product of the vectors
element_wise_product = A .* B;
% Calculate the dot product of the vectors
dot_product = dot(A, B);
% Display the results
fprintf('The sum of the vectors A and B is: [%d, %d, %d]\n', sum_vectors); fprintf('The element-wise product of the vectors A and B is: [%d, %d, %d]\n', element_wise_product); fprintf('The dot product of the vectors A and B is: %d\n', dot_product);