MATLAB

UNIUYO CSC MATLAB PRACTICAL 4 SOLUTION

Type the following program segments on the left of the table below, and give its output on the space provided on the right

Program segmentOutput
j = 150; while j > 0 j       j=j-15;       k=j^2 endj150135120105Get the rest
k1822514400110258100Get the rest

This MATLAB program defines a variable j with an initial value of 150. It then enters a while loop that continues as long as the value of j is greater than 0.

Within the loop:

  1. The current value of j is displayed.
  2. j is decremented by 15 (j = j – 15).
  3. The square of the updated value of j is calculated and assigned to the variable k (k = j^2). The loop continues until j becomes less than or equal to 0.

Let’s break down the loop iterations:

Iteration 1:

  • j is initially 150.
  • Display 150, decrement J to 135, calculate k = 135^2

Iteration 2

  • Display 135, decrement J to 120, calculate k = 120^2.

Iteration 3:

  • Display 120, decrement J to 105, calculate k = 105^2. and so on, until j becomes less than or equal to 0.

The loop will continue until j becomes negative, at which point it will exit, and the program will finish executing. The output of this program will be a series of displayed values of j and the corresponding values of k for each iteration of the loop.

Program segmentOutput
for p = 5:-1:1     
for q = 10:-2:2     
g(q, p) = p* q; end
g
  g =      0       0       0       0       0            
2       4       6       8      10           
 0       0       0       0       0            
4       8      12    16      20            
0       0       0       0       0           
 6      12     18     24     30            
 0      0       0       0       0             
8    16      24    32     40             
0     0       0       0        0            
10    20    30     40      50
  1. The outer loop (for p = 5:-1:1) iterates over values of p from 5 to 1 in steps of -1. This means p will take the values 5, 4, 3, 2, and 1.
  2. The inner loop (for q = 10:-2:2) iterates over values of q from 10 to 2 in steps of -2. This means q will take the values 10, 8, 6, 4, and 2.
  3. Inside the inner loop, the statement g(q, p) = p * q; assigns the product of p and q to the element at the q-th row and p-th column of the matrix g.
  4. The matrix g is displayed after each iteration of the inner loop. However, note that the display statement (g) is inside the inner loop, so the matrix g will be displayed multiple times during the execution of the inner loop.
  5. After both loops complete, the final matrix g will be the result of the nested loop computations.

Let’s consider a specific example of one iteration:

  • When p = 5 and q takes values from 10 to 2 in steps of -2, the elements of the g matrix will be calculated as follows:
  • g(10, 5) = 5 * 10
    • g(8, 5) = 5 * 8
    • g(6, 5) = 5 * 6
    • g(4, 5) = 5 * 4
    • g(2, 5) = 5 * 2

This process is repeated for each value of p in the outer loop, resulting in the final matrix g.

Program segment
Words = (‘ In Politics, Nemesis is a case whereby The fate of one political party is decided by another’) New_message = [‘ma’]
for i = 10:-1:2     
New_message =[ New_message, words(i*2)];     
disp(New_message)
end
end
New_message =
ma
mas
mass
massm
massmN
massmN,
massmN,c
massmN,ct
massmN,ctl
massmN,ctlP
  1. The variable Words contains a string of text.
  2. The variable New_message is initialized with the string ‘ma’.
  3. The for loop (for i = 10:-1:2) iterates over values of i from 10 to 2 in steps of -1.
  4. Inside the loop:
  • i*2 is used to calculate an index in the string Words.
    • Words(i*2) extracts a character from the string at the calculated index.
    • The extracted character is then concatenated to the existing content of New_message using square brackets ([…]).
    • The updated New_message is displayed using the disp function.
  • The loop continues to iterate, and in each iteration, a new character is added to New_message from the Words string.

Let’s consider a specific example of one iteration:

When i = 10, i*2 is 20. The character at index 20 in the Words string is ‘s’. Therefore, ‘s’ is added to New_message, and the updated New_message is displayed.

This process is repeated for each value of i in the loop, resulting in a sequence of displayed strings as characters are extracted from the Words string and added to New_message. The loop stops when i is no longer in the specified range (2 or less).   

                                                     

uniuyo csc matlab practical

Write a MATLAB program to generate 5 random numbers between -10 and 10. the MATLAB function that will help you generate the random numbers is called rand();

i) find the square of the sum of the random numbers

ii) Return the absolute value for the product of maximum and minimum random numbers.?

iii) Return the cosine of the absolute value

MATLAB random numbers

The Output

MATLAB rand number output

The randi function is used to generate random integers. length(range) provides the number of elements in the range, and 1 specifies that we want to generate one row of random numbers. The 5 at the end specifies that we want to generate 5 random numbers.

sum(randomNumb) is a sum function that calculates the sum of random numbers

max(randomNumb) is use to get the maximum random number

min(randomNumb) is use to get the minimum number

abs(maxNumber * minNumber) will get the absolute value of the product of minimum and maximum random numbers

cos(absProduct) will get the cosine of the absolute value



Write a MATLAB program to create 7 rows and 5 columns of natural number

MATLAB
THE OUTPUT

i is the number of rows and j is the number of column, the nested loop take care of the rows and column. while i runs for just 1 time j runs for 5 times filling in the value of the natural number from when count = 1 to when count =5 in row 1 and start row 2 when n = 6 to when n =10 till it gets to the last row.


write a MATLAB program to sum the series 1/2, 2/4, 3/5, 4/6, 5/7,…,n/(n+2), where n = 10

uniuyo csc practical
MATLAB PRACTICAL

matlab question
MATLAB code

Write a MATLAB code that computes the nth term of the arithmetic progression

an = a+(n-1)d; where d = 4, a = 1, and n = 5,10,15,20

the code
UNIUYO CSC MATLAB PRACTICAL

write a MATLAB script to output the sum of first 20 odd numbers

matlab

The content provided on this website is intended solely for educational purposes and as a study guide. Users are explicitly prohibited from copying, reproducing, or directly incorporating the material into manuals, assignments, or any form of assessment. The information presented here is designed to aid understanding and facilitate learning. It is not a substitute for individual effort, critical thinking, and academic integrity.

Users are encouraged to engage with the content responsibly, actively participate in their own learning process, and adhere to the academic policies and guidelines set forth by their educational institution. Any misuse or violation of these terms may result in academic consequences.

By accessing and using the content on this website, you acknowledge and agree to abide by these terms and conditions. also study other practical on webzalo schools

As a student you can read reviews on how to earn some cash that ca assist you in your academic career financially at bensfola reviews

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *