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

LAB-04-functions in MATLAB-solutions Exos1-4

Uploaded by

hufuo0faypi0
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)
58 views

LAB-04-functions in MATLAB-solutions Exos1-4

Uploaded by

hufuo0faypi0
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/ 7

University of frères Mentouri Engineering: Sciences and Technologies

Faculty of Science and Technology Cours of MATLAB


Department of electronics Academic-Year: 2023-2024

LAB#4: FUNCTIONS IN MATLAB


Exercise 01: Solve the following expressions by using built-in functions in MATLAB:
1) 𝑦 = 𝑒 −𝑎∗2 𝑠𝑖𝑛(𝑥) + 10√|𝑥 2 − 2| for a=5, x=2 and y=8.
2) 𝑦 = √𝑥 − 𝑒 −𝑎∗𝑥 √𝑙𝑜𝑔(𝑎) + 𝑙𝑜𝑔⁡(𝑏) for a=5, b=7 and x=2.
1 1 1
3) 𝑦 = 𝑠𝑖𝑛 ( ) + 𝑐𝑜𝑠(3𝑥) + ⁡𝑡𝑎𝑛(5𝑥) for x=8.
𝑥 3 5

(𝑒 𝑥 ) 𝑒 −𝑎 𝑒𝑎
4) 𝑦 = 𝑙𝑜𝑔10 × [ −2∗4 ] for x=10 and a=0.5
𝑒 𝑒 2∗𝑎
Solution of exercise 01

% expression 01: we use functions: exp, sqrt, sin and abs


>> a=5; x=2; y=8;
>> y=exp(-a*2)*sin(x)+10*sqrt(abs(x^2-2))
y =
14.1422

% expression 02: we use functions sqrt, exp and log


>> a=5; b=7; x=2;
>> y=sqrt(x)-exp(-a*x)*sqrt(log(a)+log(b))
y =
1.4141

% expression 03: we use functions sin, cos and tan


>> x=8;
>> y=sin(1/x)+(1/3)*cos(3*x)+(1/5)*tan(5*x)
y =
0.0426

% expression 4: we use functions sqrt, exp and log


>> x=10; a=0.5 ;
>> y=log10*exp(x)*[exp(-a), exp(a) ; exp(-a*4), exp(2*a)]
y =
0.0426

Exercise 02: Type the functions bellow in script files, run them with the values (U=[1,2], V=[3,4], and a=5), and
explain their functionality.
mul = @(x,y) 10*sqrt(x*y); function r = ps1(U, V) function Y=ps2(U, a)
res1=mul(2,3) n = size(U,2); [m,n] = size(U);
res2=mul(4,3) r = 0. ; Y=a*ones(1,2*n);
res3=mul(4,5) for i = 1:n, Y(2 : 1+m , n+1 : end) = U;
x=[1,2,3].*mul(7,3) r = r + U(i) * V(i); end
end

Solution of exercise 02
Function 01: This anonymous function evaluates the expression 𝑚𝑢𝑙(𝑥, 𝑦) = 10√𝑥 ∗ 𝑦 , then run it for:

o x=2 and y=3 → mul(2, 3)→ res1= 24.4949


o x=4 and y=3 → mul(4, 3)→ res2= 34.6410
o x=4 and y=5 → mul(4, 3)→ res3= 44.7214

1|Page
University of frères Mentouri Engineering: Sciences and Technologies
Faculty of Science and Technology Cours of MATLAB
Department of electronics Academic-Year: 2023-2024

Then, this function is used to compute the multiplication of a vector with a scare 𝐴.∗ 𝑎, where: A= [1,2,3] and
a=mul(7,3) → a=45.8258 and x=[ 45.8258 91.6515 137.4773]

The script file for this function is named “myscript.m” as shown in the figure below:

Run this function in the command window in order to understand how it works, as shown in the figure below:

Function 02: This function calculates 𝑟 = ∑𝑛𝑖=1 𝑈𝑖 × 𝑉𝑖 , where U and V are two input vectors, and n is the size of
the input vector U. (the statement n=size(U,2) assign to n the number of columns of the input vector U).

The script file for this function is named “ps1.m” as shown in the figure below:

Run this function in the command window in order to understand how it works, as shown in the figure below:

2|Page
University of frères Mentouri Engineering: Sciences and Technologies
Faculty of Science and Technology Cours of MATLAB
Department of electronics Academic-Year: 2023-2024

Function 03: This function takes a matrix U and a scalar a as inputs. Then, creates a row vector Y with a length
of 2*n where every element is equal to a. Then, it replaces a specific subset of Y with the values from the input
matrix U. The modified Y is then returned as the output. Specifically:

o Y=a*ones(1,2*n);: This line creates a row vector Y of length 2*n, where each element is equal to a. In
other words, it initializes Y as a row vector with 2*n elements, all set to the value of a.
o Y(2 : 1+m , n+1 : end) = U;: This line of code assigns the values of the input matrix U to a subset of Y.
Specifically, it replaces the elements in Y starting from the second row to the first row plus m, and
starting from the n+1 column to the last column with the values from U.
The script file for this function is named “ps2.m” as shown in the figure below:

Run this function in the command window in order to understand how it works, as shown in the figure below:

1 2
This is another example for 𝑈 = ( ) and a=7, in order to understand how this function work.
3 4

3|Page
University of frères Mentouri Engineering: Sciences and Technologies
Faculty of Science and Technology Cours of MATLAB
Department of electronics Academic-Year: 2023-2024

Exercise 03: use the MATLAB command line to write and run anonymous functions for the following statements:
1) Display on the screen the message “This is an anonymous function”
2) Check if a number is even or odd using the mod function.
3) Evaluate the expression: 𝑥 3 + 𝑥 √𝑦 − 𝑧
1
4) Solve the equation 𝑔(𝑐) = ∫0 (𝑥 2 + 𝑐𝑥 + 1)𝑑𝑥 by combining two anonymous functions.
5) Display the values greater than 50 in the array A = [ 120, 45, 67, 23, 89, 340, 17]
Solution of exercise 03
% MATLAB code for anonymous function 01
>> print = @() disp("This is an anonymous function.");
>> print();
Run the function in the command window in order to see the result, as shown in the figure below:

% Anonymous function 02: Check if a number is even or odd using the mod function
>> check_even_odd = @(number) mod(number, 2) == 0;
>> is_even = check_even_odd(6); % Returns true (1) (because 6 is even)
>> is_even = check_even_odd(7); % Returns false (0) (because 7 is odd)

Run the function in the command window in order to see the result, as shown in the figure below:

% Anonymous function 03: anonymous function for expression (Question 3)


>> expression =@(x, y, z) x^3+x*sqrt(y)-z;
>> result=expression(1, 5, -10)
result =
13.2361

Run the function in the command window in order to see the result, as shown in the figure below:

4|Page
University of frères Mentouri Engineering: Sciences and Technologies
Faculty of Science and Technology Cours of MATLAB
Department of electronics Academic-Year: 2023-2024

Anonymous function 4 (Multiple Anonymous Functions): This example solves the following equation by
1
combining two anonymous functions: 𝑔(𝑐) = ∫0 (𝑥 2 + 𝑐𝑥 + 1)𝑑𝑥

Note: The expression in an anonymous function can include another anonymous function. This is useful for
passing different parameters to a function that you are evaluating over a range of values.
The equivalent anonymous function for this expression can be written as follow:
1) First, take the parenthesized part of the equation (the integrand) and write it as an anonymous function.
You do not need to assign the output to a variable as it will only be passed as input to the quad function.
>> @(x) (x.^2 + c*x + 1)

2) Next, evaluate this function from zero to one by passing the function handle, shown here as the entire
anonymous function, to quad.
>> quad(@(x) (x.^2 + c*x + 1), 0, 1)

3) Supply the value for c by constructing an anonymous function for the entire equation as follw:
>> g = @(c) (quad(@(x) (x.^2 + c*x + 1), 0, 1));
>> >> g(2)
ans =
2.3333

Run the function in the command window in order to see the result, as shown in the figure below:

Anonymous function 05: Display the values greater than 50 in the array A=[120, 45, 67, 23, 89, 340, 17]
>> A = [120, 45, 67, 23, 89, 340, 17];
>> % Define a function to filter values greater than 50
>> filterFunc = @(x) x > 50;

>> % Filter data


>> filteredata = A(arrayfun(filterFunc, A));
filteredata =
120 67 89 340

5|Page
University of frères Mentouri Engineering: Sciences and Technologies
Faculty of Science and Technology Cours of MATLAB
Department of electronics Academic-Year: 2023-2024

Note: arrayfun is a MATLAB function that applies a specified function to each element of one or more arrays,
and returns the result as an array of the same size. It is a very useful tool for performing element-wise operations
on arrays without using explicit loops.
For example, consider a simple function that squares a number:

function result = square(x)


result = x^2;
end

You can use arrayfun to apply this function to an array:


>> A = [1, 2, 3, 4];
>> B = arrayfun(@square, A)
B =
1 1 1 -1

Exercise 04: Use a script file to write and save a main function named 'state' that computes the length of a list and
call the subfunctions 'mean' and 'median', providing them with the length of the list. The subfunctions 'mean' and
'median' then compute the average and median of the given list, respectively.
o The "average" number is found by adding all number and dividing by the number of numbers in the set.
o The middle number (median) is found by ordering all numbers and picking out the one in the middle (or
if there are two middle numbers, taking the mean of those two numbers). Example: the median of {4, 1,
7} is 4 because when the numbers are put in order {1, 4, 7}, the number 4 is in the middle.
Use the command line of MATLAB to run this script file for the set of numbers S = {10, 60, 14, 7, 22, 3} and
report the results.
Solution of exercise 04
Code of the function “stats" that should be written and saved in the script file named “stats.m”

function [avg, med] = newstats(u) % Primary function


% NEWSTATS Find mean and median with internal functions.
n = length(u);
avg = mean(u, n);
med = median(u, n);

function a = mean(v, n) % Subfunction


% Calculate average.
a = sum(v)/n;

function m = median(v, n) % Subfunction


% Calculate median.
w = sort(v);
if rem(n, 2) == 1

6|Page
University of frères Mentouri Engineering: Sciences and Technologies
Faculty of Science and Technology Cours of MATLAB
Department of electronics Academic-Year: 2023-2024

m = w((n+1) / 2);
else
m = (w(n/2) + w(n/2+1)) / 2;
end
Code for running the function in the command window
>> u=[10, 60, 14, 7, 22, 3];
>> [avg, med]=stats(u)
avg = 19.3333
med = 12
The script file for this function is named “stats.m” as shown in the figure below:

Run the function in the command window in order to see the result, as shown in the figure below:

7|Page

You might also like