0% found this document useful (0 votes)
36 views

Answer All Questions:: Solution

This document provides solutions to 7 questions about using MATLAB for numerical analysis and programming. It demonstrates how to find polynomial roots, evaluate functions, assign letter grades based on numerical scores, calculate mean and standard deviation, and use for loops and if/else statements to analyze student academic levels and output values.

Uploaded by

Ayoub Mohamed
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)
36 views

Answer All Questions:: Solution

This document provides solutions to 7 questions about using MATLAB for numerical analysis and programming. It demonstrates how to find polynomial roots, evaluate functions, assign letter grades based on numerical scores, calculate mean and standard deviation, and use for loops and if/else statements to analyze student academic levels and output values.

Uploaded by

Ayoub Mohamed
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/ 4

Tutorial CSC209 Controls & Loops

Answer all questions:


1. Find the roots of the polynomial equation
y(x) = x6 - x5 - 6x4 + 4x3 - 12x2
Solution:

clear
clc
syms x
y= x^6 - x^5 - 6*x^4 + 4*x^3 - 12*x^2
p=[1 -1 -6 4 -12 0 0];
roots(p)

2. Write a MATLAB program to evaluate a function f(x,y) for any two user-specified values
x and y. The function f(x,y) is defined as follows.

Solution:

% Prompt the user for the values x and y


x = input ('Enter the x coefficient: ');
y = input ('Enter the y coefficient: ');
% Calculate the function f(x,y) based upon
% the signs of x and y.
if x >= 0 && y >= 0
fun = x _ y;
elseif x >= 0 && y < 0
fun = x _ y^2;
elseif x < 0 && y >= 0
fun = x^2 _ y;
else % x < 0 and y < 0, so
fun = x^2 _ y^2;
end
Tutorial CSC209 Controls & Loops

3. Suppose that we are writing a program which reads in a numerical grade and assigns a
letter grade to it according to the following table:

Solution:

if grade > 95.0


disp('The grade is A.');
elseif grade > 86.0
disp('The grade is B.');
elseif grade > 76.0
disp('The grade is C.');
elseif grade > 66.0
disp('The grade is D.');
else
disp('The grade is F.');
end
4. Check the possible structure of the program below, state the missing part and justify what will happen?

The missing part is: …end………………………….

Justify what will happen? ……error…………………


Tutorial CSC209 Controls & Loops

5. Use three input values: 3, 4, and 5, to calculate the mean and standard deviation of these values using
Matlab Program.

clear
clc
% Define variables:
% n -- The number of input samples
% std_dev -- The standard deviation of the input samples
% sum_x -- The sum of the input values
% x -- An input data value
% xbar -- The average of the input samples
x1=[3 4 5];
x=[3 4 5];
n=length(x)
x_bar = sum(x)/length(x)

std_dev = sqrt((n*sum(x) - (sum(x))^2) / (n*(n-1)))

6. Develop a Matlab Program to know the level of the student in the academic semester and
year automatically.
clear
clc
% Age of the student in year
age=input('Input the target age: ')
if age>=25
disp('This student already graduated')
elseif age>=24
disp('This student study in level 10 at the 4th year')
elseif age>=23.5
disp('This student study in semester 9 at the 4th year')
elseif age>=23
disp('This student study in semester 8 at the 3rd year')
elseif age>=22.5
disp('This student study in semester 7 at the 3rd year')
elseif age>=22
disp('This student study in semester 6 at the 2nd year')
elseif age>=21.5
disp('This student study in semesrae 5 at the 2nd year')
elseif age>=21
disp('This student study in semester 4 at the 1st year')
elseif age>=20.5
disp('This student study in semester 3 at the 1st year')
else
disp('This student study in the Preparatory Year or not
enter the university yet')
end
Tutorial CSC209 Controls & Loops

7. What is the output of the program below

The Output: 1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
End of inner loop
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
End of inner loop
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
End of inner loop
End of outer loop

for a = 10:20
fprintf('value of a: %d\n', a);
end

The Output: value of a: 10


value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
value of a: 20

No output

You might also like