0% found this document useful (0 votes)
7 views3 pages

Exercise 2-New

The document contains a series of exercises related to MATLAB programming. It includes tasks such as finding the root of an equation, explaining a code snippet that randomizes a vector, writing a function to compute the element-wise maximum of two matrices, and calculating the sum of an array using both for and while loops. Additionally, it provides a program to display elements of a matrix that are greater than 3 based on user input.

Uploaded by

ngọc nguyễn
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)
7 views3 pages

Exercise 2-New

The document contains a series of exercises related to MATLAB programming. It includes tasks such as finding the root of an equation, explaining a code snippet that randomizes a vector, writing a function to compute the element-wise maximum of two matrices, and calculating the sum of an array using both for and while loops. Additionally, it provides a program to display elements of a matrix that are greater than 3 based on user input.

Uploaded by

ngọc nguyễn
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/ 3

Exercise 2

Q1. Use Matlab as a calculator to find the root of the equation


0.5(𝑥 − 2)3 − 40𝑠𝑖𝑛𝑥 = 0
within the interval [2,4]. Show the 2 decimal places answer.

Q2. What does the following Matlab code do?


x=[1,1,2,-2,3,-3,4,-4];
N=length(x);
m=randperm(n);
x=x(m);
This MATLAB code performs the following operations:
Define a vector x with elements [1, 1, 2, -2, 3, -3, 4, -4].
Determine the length of vector x and store it in the variable N.
Generate a random permutation of integers from 1 to N using the randperm function. The
permutation is stored in the vector m.
Reorder the elements of vector x based on the random permutation stored in vector m.

Q3. Write a Matlab function that will take into two matrices A,B and will output a matrix of the
same size where
C(i,j)=max{A(i,j),B(i,j)}
function C = elementwise_max(A, B)
% Check if the input matrices have the same size
if ~isequal(size(A), size(B))
error('Input matrices must have the same size.');
end

% Element-wise maximum operation


C = max(A, B);
end

% Example matrices A and B


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

% Call the function


C = elementwise_max(A, B);

% Display the result


disp('Matrix C:');
disp(C);

Q1. Given that


X=[10, -20,30,50,5,25]. Calculate the sum of the given array x; Calculate the sum using for loop
and while loop
Ans
>>X=10,-20,30,50,5,25]
Use for loop:
Sum=0;
For i=1:length(x)
Sum=sum+x(i);
End
Disp(sum)
Use while loop:
Sum=0;i=1;
while i<=length(x)
Sum=sum+x(i);
i=i+1;
End

Q2. Write a Matlab program that will take a matrix from user input and display all elements of
the matrix that are larger than 3.

Ans.
A=input(‘matrix:’)
dimension=size(A);
[row column]=size(A);
for i=1:row
for j=1:column
If A(i,j)>3
Disp(A)
end
end
end

You might also like