LAB-04-functions in MATLAB-solutions Exos1-4
LAB-04-functions in MATLAB-solutions Exos1-4
(𝑒 𝑥 ) 𝑒 −𝑎 𝑒𝑎
4) 𝑦 = 𝑙𝑜𝑔10 × [ −2∗4 ] for x=10 and a=0.5
𝑒 𝑒 2∗𝑎
Solution of exercise 01
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:
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:
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;
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:
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”
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