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

Tp4-Matlab Function

The document provides instructions for several MATLAB exercises involving user-defined functions. It includes tasks to: 1) Define functions to calculate the area of a circle and volume of a cylinder from user input. 2) Use the built-in hypot() function to check if a triangle is a right triangle and calculate the length of a line segment. 3) Define a custom my_hypot() function to replace hypot(). Additional exercises involve discussing local and global variables in functions and defining functions for common mathematical operations.

Uploaded by

Fayza 1008
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)
63 views3 pages

Tp4-Matlab Function

The document provides instructions for several MATLAB exercises involving user-defined functions. It includes tasks to: 1) Define functions to calculate the area of a circle and volume of a cylinder from user input. 2) Use the built-in hypot() function to check if a triangle is a right triangle and calculate the length of a line segment. 3) Define a custom my_hypot() function to replace hypot(). Additional exercises involve discussing local and global variables in functions and defining functions for common mathematical operations.

Uploaded by

Fayza 1008
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

Setif -1- University

TP4- Matlab- Programming mode Computer Science III


functions ING-ST 2nd year
Faculty of Technology
EBT Department 1 session (3h) A.Y:2023/2024
______________________________________________________________________________________________

Objectif : Mastering Matlab-functions


part I.
Task 1:( understanding matlab- functions)
Given the following two M-files :
main.m sqr.m
1 x = input('Enter x: '); 1 function y = sqr(x)
2 y = sqr(x); 2 y = x * x;
3 fprintf("%.2f ^ 2 = %.2f\n",x, y); 3 end

Questions :
1. Edit these two files in the same current folder and execute main.m.
2. What is the content of the sqr.m file?
3. What is the purpose of the statement on line 2 in main.m?
4. Describe the meaning and usage of variables x and y in sqr.m.
5. Explain the distinctions between the sqr function, fprintf, and input.
6. Compare the variables x and y defined within the sqr function with those in the main script.
7. After line 3 in main.m, insert the following code:
V = [1 2 3];
yy = sqr(v);
disp(yy);
Execute the script and report your observations. If there are any issues, provide guidance on how to
resolve them.
8. Can you redefine the sqr function within main.m using the @ anonymous function notation?

Task 2: :( writing user-defined functions)


1. Area of a Circle:
a. Define a function called "area" that takes the radius "r" as a parameter and returns
the area. r
b. Read the radius "r" from the keyboard (user input) and call the "area" function to
calculate the area of the circle.
2. Volume of a Cylinder:
r
a. Define a function called "volume" that takes the radius "r" and height "h" as
parameters and returns the volume.
h
b. Read the values of radius "r" and height "h" from the keyboard (user input) and
call the "volume" function to calculate the volume of the cylinder.

1
Part II

Task 3 (Using the hypot() Built-in Function):

1. Get Information About hypot():


Go to the help documentation for the hypot() built-in function and find out:
a. How many inputs it needs.
b. How many outputs it returns.
c. How outputs are calculated from inputs.
2. Check for a Right-Angle Triangle:
Use the hypot() function to check if a given triangle is a right-angle triangle (‫)مثلث قائم‬. You'll
a b
need to input the lengths of its sides a, b, and c from the keyboard. Test your script with the
values a=5, b=3, c=4. c
3. Calculate the Length of a Segment:
Utilize the hypot() function to calculate the length "L" of a segment. The 𝒂(𝒙𝒂, 𝒚𝒂) 𝒃(𝒙𝒃, 𝒚𝒃)
coordinates of points a and b (xa, ya, xb, yb) should be entered from the 𝑳

keyboard.

Task 4 : (user-defined function )


Suppose there is no hypot(the built-in function) available. In order to create a user-defined
y b
function, follow these steps:
1. Create a function file called my_hypot.m and define a function named my_hypot that takes a
two parameters, a and b. This function should return the length of the hypotenuse y using the
Pythagorean theorem: 𝑦 = √𝑎2 + 𝑏 2
2. Create a script file named main.m and add the necessary statements to use the my_hypot function to
answer questions b and c of Task 3:

main.m my_hypot.m
clc function …… = my_hypot(………………)
clear
……………………………… …………………………………………
………………………………….. ………………………………………….
………………………………
end

In main.m script, make sure to incorporate the input of values for xa, ya, xb, and yb from the keyboard to
solve questions b and c of Task 3.

2
Additionnal exercises

Exercice 1 : (local/Global Variables)

Discuss the value of ii for each code:

% main.m % main.m % main.m % main.m

ii = f1(1); ii=2; ii=4; global ii;


fprintf("%d",ii); f2(); f3(); ii=4;
printf("%d",ii); fprintf("%d",i); f4();
fprintf("%d",ii);
% f1.m % f2.m % f3.m % f4.m

function y= f1(x) function f2() function f3() function f4()


ii=5; ii=5; fprintf("%d",ii); global ii;
y = x+ii; end end ii=7;
end end

Exercice 2 : (more user-defined function )

Write a script/function Matlab that permit:

1- Define the function f_pow(x, n) to calculate 𝒙𝒏 ; 𝒙 𝝐 𝑹+ 𝒂𝒏𝒅 𝒏 𝝐 𝑵.


2- Define the function f_fact(n) to calculate 𝒏! ; 𝒏 𝝐 𝑵.
3- Define the function f_exp(x) that calculate 𝒆𝒙 , 𝒙 𝝐 𝑹.
4- Define the function f_cos(x) that calculate 𝒄𝒐𝒔(𝒙) , 𝒙 𝝐 𝑹.
5- Define the function f_sin(x) that calculate 𝒔𝒊𝒏(𝒙) , 𝒙 𝝐 𝑹.
6- Call the functions as defined above and compare with the Matlab built-in functions (power(),
factorial(), exp(), cos(), sin())

Reminder :

.𝒙𝒏 = 𝒙 × 𝒙 × 𝒙 × … . .× 𝒙 n times multimlication

.𝒏! = 𝟏 × 𝟐 × 𝟑 × … . .× (𝒏 − 𝟏) × 𝒏

𝒙𝟐 𝒙𝟑 𝒙𝒌
.𝒆𝒙 = 𝟏 + 𝒙 + + + ...+ + . . . with 𝒌 = 𝟎. . 𝒏 /number of terms
𝟐! 𝟑! 𝒌!

𝒙𝟐 𝒙𝟒 𝒙𝟔 (−𝟏)𝒌 𝒙𝟐𝒌
.𝒄𝒐𝒔(𝒙) = 𝟏 − + − + ...+ (𝟐𝒌)!
+ . . . with 𝒌 = 𝟎. . 𝒏 / number of terms
𝟐! 𝟒! 𝟔!

𝒙𝟑 𝒙𝟓 𝒙𝟕 (−𝟏)𝒌 𝒙𝟐𝒌+𝟏
.𝒔𝒊𝒏(𝒙) = 𝒙 − 𝟑!
+ 𝟓!
− 𝟕!
+ ...+ (𝟐𝒌+𝟏)!
+ . . . with 𝑘 = 𝟎. . 𝒏 / number of terms

You might also like