Computer-T17-Solution Assignment-OK - 230418 - 042205
Computer-T17-Solution Assignment-OK - 230418 - 042205
Computer/3
Lect.17/Assignment Solution
Third Year
❑ The character \n in form ‘\n\n’ use to get blank line in output, for example:
>> fprintf ('Hello \n\n This is MATLAB \n')
Hello
This is MATLAB
❑ field width can also be included in the placeholder in fprintf, which specifies how many
characters total are to be used in printing.
▪ %5d would indicate a field width of 5 for printing an integer and
▪ %10s would indicate a field width of 10 for a string.
▪ %6.2f means a field width of 6(including the decimal point and the decimal places)
with two decimal places.
▪ %.3f indicates three decimal places.
Assignment /1/Lec.12 :
Solution
clear;
clc;
x=input('string =','s')
double(x)
• Convertcode
string =IRAQ
x=
'IRAQ’
105 114 97 113
Assignment /1/Lec.12 :
2. If x=[1 5 9; 2 7 4], then:
a) display the last two elements by using disp command.
b) display the sum of each row as show below
The sum of 1st row =
The sum of 2nd row =
Solutiona)
x=input('parameter=‘)
x=input('parameter=')
disp(x(2,2))
disp(x(2,3))
b)
y1=sum(x(1,:));
y2=sum(x(2,:));
disp(['The sum of 1st row =',num2str(y1)])
disp(['The sum of 2st row =',num2str(y2)])
>> ass
parameter=[1 5 9;2 7 4];
x=
1 5 9
2 7 4
7
4
The sum of 1st row =15
The sum of 2st row =13
Assignment /1/Lec.12 :
Solution
x=0:0.01:2*pi;
y1 = 2* cos(x);
y2 = cos(x);
y3 = 0.5 *cos(x);
plot(x,y1,'--r',x,y2,'-.g',x,y3,'-b')
xlabel('x-axis')
ylabel ('y-axis')
title('use plot function in M-file')
grid
gtext('y1')
gtext('y2')
gtext('y3’)
Assignment /2/Lec.13 :
>> Anglecosine
The cosine of angle 0 is :1.000
The cosine of angle 15 is :0.966
The cosine of angle 30 is :0.866
The cosine of angle 45 is :0.707
The cosine of angle 60 is :0.500
The cosine of angle 75 is :0.259
The cosine of angle 90 is :0.000
Assignment /2/Lec.13 :
2: write a program (vector.m) to generate a vector with 12 random elements and find:
a. The largest element and its position.
b. The smallest element and its position.
Solution
clc
clear;
format bank
v=rand(1,12);
[vmax,pmax]=max(v)
[vmin,pmin]=min(v)
>> vector
vmax =
0.97
pmax =
12.00
vmin =
0.10
pmin =
6.00
Assignment /2/Lec.13 :
3. Write a program to test the degrees of 40 students and find: (namedfile “students”)
1) The number of students those degrees above 80
2) The number of students those degrees between 60 and 79
3) The number of students those failed and their degrees
Solution
clear; clc;
Degree= randi([25,100],1,40); % generate the degrees of 40 students their range (25-100)
y=Degree'
DegreeAbove80=sum(Degree>80); % find no. of students which degree>80 using sum function
9.00
Failures =
12.00
------------------------------------------------
The no. of students with degree above 80 : 9
The no. of students with degrees 60 to 79 : 13
The no. of failures students: 12
Columns 9 through 12
>>
Assignment /3/Lec.14 :
1. Write a program to compeer between the value A and B ,when Ais greater than B
it is display a message (‘greater’) ,when A is less than B it is display a
message(‘less’) and when A is equal B it is display a message(‘equal’): (named
file “comparevalue”)
2. write a program to find the Y value when: (named file “Zvalue”)
𝟐
−𝟏𝟎 ≤ 𝐱 < 𝟎
𝐱𝟑
𝐙= 𝟐 𝐱=𝟎
𝟑
𝐱𝟐 + 𝟒 𝟎<𝐱≤𝟕
3. Write a program to display the name of day by giving the day number as the
following:
No. Day
1 Saturday
2 Sunday
3 Monday
4 Tuesday
5 Wednesday
6 Thursday
Solution :
A=input('A=');
B=input('B=');
if A > B
'greater'
elseif A < B
'less' >> comparevalue
A=500
elseif A == B B=5000
'equal' ans =
else 'less'
>> comparevalue
error ('Unexpected situation') A=20
end B=2
ans =
'greater'
Assignment /3/Lec.14 :
2:write a program to find the Y value when: (named file “Zvalue”)
𝟐
−𝟏𝟎 ≤ 𝐱 < 𝟎
𝐱𝟑
𝐙= 𝟐 𝐱=𝟎
𝟑
𝟐
𝐱 +𝟒 𝟎<𝐱≤𝟕
Solution :
x=input('Enter the x value : '); >> Zvalue
if x<-10 | x>7 Enter the x value : 9
disp('Undefined the Y value') Undefined the Y value
elseif x>=-10 & x<0 >> Zvalue
Enter the x value : 0
Y=2/x^3;
The Y value = 2
fprintf('The Y value = %0.3f\n ',Y); x >> Zvalue
elseif x>0 & x<=7 Enter the x value : -5
Y=nthroot(sqrt(x^2+4),3); The Y value = -0.016
fprintf('The Z value = %0.3f\n ',Y); >> Zvalue
else Enter the x value : 1
fprintf('The Z value = %d\n ',2); The Y value = 1.308
end
𝟐
−𝟏𝟎 ≤ 𝐱 < 𝟎
𝐱𝟑
𝐙= 𝟐 𝐱=𝟎
𝟑
𝐱𝟐 + 𝟒 𝟎<𝐱≤𝟕
>> Zvalue
Enter the x value : 9
Undefined the Y value
>> Zvalue
Enter the x value : 0
The Y value = 2
x >> Zvalue
Enter the x value : -5
The Y value = -0.016
>> Zvalue
Enter the x value : 1
The Y value = 1.308
Assignment /3/Lec.14 :
3: Write a program to display the name of day by giving the day
number as the following:And handle the invalid number (named file “dayname”