# Matlab Exercises

It’s great time to hit the road running. We have got enough Matlab lessons to start handing you some nice Matlab challenges to help you to assess yourself.

Here are some Matlab exercises.

## Matlab Exercises

### Exercise 1

1. Write a Matlab code corresponding to the above structogram.

2. What are the value of a, b and c at the end of this program.

### Exercise 2

Analyze the following code

```x=1;
y=2;
z=3;
for i=1:3
x=2*x;
while x&lt;5
y=y+1;
z=z*x;
x=y;
end
end
fprintf('x=%i \n',x);
fprintf('y=%i \n',y);
fprintf('z=%i \n',z);```

1. Without using neither a calculator nor your computer, compute the values of x, y and z at the end of this program.

2. Now check your result using Matlab. (Simply copy the code, paste in Matlab and run it)

### Exercise 3

Let’s consider a cone. Write a Matlab program that computes the volume of a cone. Here is the formula you should be using.

Where r is the radius of the base and h is the height. Write a Matlab program that finds the radius of a cone. The program should first check that r and h are both positive. The precision of the result should be at most 3 decimal digit.

### Exercise 4

Let’s consider the following function saved in the file recursive.m

```function[y]=recursive(x,y)
if (x&gt;y)
y=recursive(y,2)-1;
else
y=3;
end```

1.  What will be the result of y=recursive(9,7)?

2. What will be the result of y=recursive(9,11)?

### Exercise 5

The following Matlab programs contain some elementary programing mistakes. Find the mistake and suggest a solution to each or them.

(a)

```function[d]=pol2(h)
d=0;
i=0;
while i&lt;h
d=d+1;
end```

(b)

```function[l]=pol3(arr)
for i=1:length(arr)
diff(i)=arr(i)-arr(i+1);
end```

(c)

```r=input('Please enter a number: ');
if r=0
fprintf('The number is zero');
else
fprintf('The number is negative');```

### Exercise 6

Write a Matlab function that computes the following sum while requesting the value of x and n from the user.

To call the function, the user should use the following syntax

### Exercise 7

Consider the following Matlab program

```value=[2 5 7 -2 8 6 4 3];
Data=[4 5 3 7];
r=4;
s=1;
t=7;
x=top(value, r, t);
y=top(value, s, r);
z=value(8);
fprintf('x=%i \n',x);
fprintf('y=%i \n',y);
fprintf('z=%i \n',z);```

The function top is define the following way

```function[g]=top[ele, p, k]
g=ele(p)-ele(k);```

Without using your computer, find the value of x, y and z at the end of this program.

### Exercise 8

We say a n × n matrix A is symmetric when for all i, j=1,…,n

Let’s consider the following matrices

A is symmetric but B is not

Write a Matlab function that will test if a matrix is symmetric or not. The function will return the number “1” if the matrix is symmetric and “0” if it is not.

To call the function you should use the following syntax

Symmetric=SymmetricMatrix(A)

### Exercise 9

Solve the following equations using matlab

(a)

(b)

### Exercise 10

Graph the equation manually and plot the graph in Matlab to check your result having the following information