Exercitii MATLAB Pentru Toti
Exercitii MATLAB Pentru Toti
,:, Exercise 3 r Usino a while looP, write a MATLAB progiam that counts down from a
number n to 1.
r+
!ii;i=
Exercise 3. Solution
clear, close all;
clc; format comPact;
n=25; while(n>=1)
disp(n); n=n-1; end
r,,::iiW;:',,,:,'
Exercise 4
r Write a MATIAB program that counts all odd numbers'be-tween 1 and a
number n.
Exercise 4. Solution
clear close all
clc n=12i
count=O; for i=1:n
if(rem(i,2)-=g;
count=count+1, end end disp(count);
r.l,
i;,".:-
:'
::
:,::'
,,1
Exercise 5
r Change
betfreen
Exercise 5. Solution
the
clc
n=15;
a..
i:;.::-il i;
Exercise 6
Exercise 6. Solution
MATLAB Program that
clear, close all;
clc;
rWrite
sum:
I
I
t
1)*
(i 2-1)(i+
00));
r-,riW.::ri Exercise 7
''
l*
::
Exercise 7. Solution
n=4; for i=1:n
r Write a MATLAB Program to inPut an inteoer n and build a n bY n matrix witn-ones on the main didgonal and
zeros everywhere else.
forl=1'n
if
elseoolo A(ii)=g'
end end end
A(ij)=1;
i==j
A=
l?33
oo0'l
rr@.,,..
"w.'
Exercise 7. Solution
n=4;
Exercise 7
r Chanqe the Previous Program to the numbers 1, 2,..., n on the disolal 'diagonal and -1 everywhere main
else.
for i=1:n
for
1=1
'n
o
=l
''t 2 -1
-'r -1 3
-'l -1 -1
-., _,
_,,
-1
A(ij)=-1'
end end end