0% found this document useful (0 votes)
32 views3 pages

Exercitii MATLAB Pentru Toti

This document contains 7 exercises with MATLAB code solutions for tasks like counting down from a number, counting odd/even numbers between 1 and a given value, calculating a sum of values, and building matrices with specified diagonal and off-diagonal elements. Each exercise poses a programming problem and then provides the full MATLAB code solution.

Uploaded by

Codrin Enea
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)
32 views3 pages

Exercitii MATLAB Pentru Toti

This document contains 7 exercises with MATLAB code solutions for tasks like counting down from a number, counting odd/even numbers between 1 and a given value, calculating a sum of values, and building matrices with specified diagonal and off-diagonal elements. Each exercise poses a programming problem and then provides the full MATLAB code solution.

Uploaded by

Codrin Enea
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/ 3

r.=,W;.;'",'fi.

,:, 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

Previous MATLAB oroordm to counl all even numbers


1 and a number n.

the

clear close all

clc
n=15;

count=0, for i=1:n


if(rem(i,2)==Q1

count=count+1; end end disp(count);

a..

i:;.::-il i;

Exercise 6

Exercise 6. Solution
MATLAB Program that
clear, close all;
clc;

rWrite
sum:

calculates and displays the following

I
I
t

n=10000; J=0; for i=1:n J=J+sqrt((i"2+ end disp(J);

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

i=-j A(ii)=i' else


if

1=1

'n
o

=l

''t 2 -1
-'r -1 3
-'l -1 -1

-., _,

_,,

-1

A(ij)=-1'
end end end

You might also like