## UNIUYO CSC MATLAB PRACTICAL 4 QUESTION 1

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

### SOLUTION WITH EXPLANATION

Program segment | Output | |||||

j = 150; while j > 0 j j=j-15; k=j^2 end | j | 150 | 135 | 120 | 105 | Get the rest |

k | 18225 | 14400 | 11025 | 8100 | Get 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:

- The current value of j is displayed.
- j is decremented by 15 (
**j = j – 15**). - 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 segment | Output |

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 |

- 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. - 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. - 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**. - 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. - 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 |

- The variable
**Words**contains a string of text. - The variable
**New_message**is initialized with the string ‘ma’. - The
**for**loop (**for i = 10:-1:2**) iterates over values of**i**from 10 to 2 in steps of -1. - 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 4 QUESTION 2

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

### SOLUTION

The Output

### QUESTION 2 EXPLANATION

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

a**bs(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

## study questions 3,4 and 5 share your experience in the comment section

## UNIUYO CSC MATLAB PRACTICAL 4 QUESTION 6

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

**SOLUTION**

### THE OUTPUT

### QUESTION 6 EXPLANATION

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.

## UNIUYO CSC MATLAB PRACTICAL 4 QUESTION 7

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

### SOLUTION

### THE OUTPUT

## UNIUYO CSC MATLAB PRACTICAL 4 QUESTION 8

### SOLUTIONS

The output should be “**9.536743e+11 is the sum of Ai**“

## UNIUYO CSC MATLAB PRACTICAL 4 QUESTION 9

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

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

### THE OUTPUT

## UNIUYO CSC MATLAB PRACTICAL 4 QUESTION 10

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

**The output is ” the sum of the first 20 odd number is 400″**

**Disclaimer:**

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