0% found this document useful (0 votes)
11 views2 pages

User-Defined Functions in MATLAB

The document describes the creation of user-defined functions in MATLAB for mathematical calculations and temperature conversions. It provides examples of a function that computes a specific mathematical expression for both scalar and vector inputs, and another function that converts Fahrenheit to Celsius while calculating the change in area of an aluminum plate due to temperature changes. The solutions demonstrate how to execute these functions in the Command Window and the results obtained from the calculations.

Uploaded by

Aymen Al Al-Baaj
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)
11 views2 pages

User-Defined Functions in MATLAB

The document describes the creation of user-defined functions in MATLAB for mathematical calculations and temperature conversions. It provides examples of a function that computes a specific mathematical expression for both scalar and vector inputs, and another function that converts Fahrenheit to Celsius while calculating the change in area of an aluminum plate due to temperature changes. The solutions demonstrate how to execute these functions in the Command Window and the results obtained from the calculations.

Uploaded by

Aymen Al Al-Baaj
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

Sample Problem 7-1: User-defined function for a math function

Write a function file (name it chp7one) for the function . The


input to the function is x and the output is . Write the function such that x can
be a vector. Use the function to calculate:
(a) for x = 6.
(b) for x = 1, 3, 5, 7, 9, and 11.
Solution
The function file for the function is:
Note that the mathematical expression in the function file is written for elementby-
element calculations. In this way if x is a vector, y will also be a vector. The
function is saved and then the search path is modified to include the directory
where the file was saved. As shown below, the function is used in the Command
Window.
(a) Calculating the function for can be done by typing chp7one(6) in
the Command Window, or by assigning the value of the function to a new variable:
(b) To calculate the function for several values of x, a vector with the values of x
is created and then used for the argument of the function.
x =
440.06
y =
158423.02
function y=chp7one(x)
y=(x.^4.*sqrt(3*x+5))./(x.^2+1).^2;
>> chp7one(6)
ans =
4.5401
>> F=chp7one(6)
F =
4.5401
>> x=1:2:11
x =
1 3 5 7 9 11
f(x) x4 3x + 5
(x2 + 1)2 = ------------------------
f(x)
f(x)
f(x)
f(x)
Function definition line.
Assignment to output argument.
x=6
228 Chapter 7: User-Defined Functions and Function Files
Another way is to type the vector x directly in the argument of the function.
Sample Problem 7-2: Converting temperature units
Write a user-defined function (name it FtoC) that converts temperature in degrees
F to temperature in degrees C. Use the function to solve the following problem.
The change in the length of an object, , due to a change in the temperature, ,
is given by: , where α is the coefficient of thermal expansion. Determine
the change in the area of a rectangular (4.5 m by 2.25 m) aluminum
( 1/ C) plate if the temperature changes from 40 F to 92 F.
Solution
A user-defined function that converts degrees F to degrees C is:
A script file (named Chapter7Example2) that calculates the change of the area of
the plate due to the temperature is:
Executing the script file in the Command Window gives the solution:
>> chp7one(x)
ans =
0.7071 3.0307 4.1347 4.8971 5.5197 6.0638
>> H=chp7one([1:2:11])
H =
0.7071 3.0307 4.1347 4.8971 5.5197 6.0638
function C=FtoC(F)
%FtoC converts degrees F to degrees C
C=5*(F-32)./9;
a1=4.5; b1=2.25; T1=40; T2=92; alpha=23e-6;
deltaT=FtoC(T2)-FtoC(T1);
a2=a1+alpha*a1*deltaT;
b2=b1+alpha*b1*deltaT;
AreaChange=a2*b2-a1*b1;
fprintf('The change in the area is %6.5f meters
square.',AreaChange)
>> Chapter7Example2
The change in the area is 0.01346 meters square.
ΔL ΔT
ΔL = αLΔT
α = 23 ⋅ 10–6 ° ° °
Function definition line.
Assignment to output argument.
Using the FtoC function to calculate the
temperature difference in degrees C.
Calculating the new length.
Calculating the new width.
Calculating the change in the area.
7.7 Comparison between Script Files and Function Files 229

Common questions

Powered by AI

Changes in area due to temperature are calculated using the formula for linear expansion `ΔL = αLΔT`, where α is the material's coefficient of thermal expansion, and ΔT is the temperature change. The concept can be applied to both length and width to compute area change. This principle leverages the linear relationship between temperature change and dimensional change in materials .

The process involves using a user-defined function `function C=FtoC(F)` that converts temperature from °F to °C with the formula `C=5*(F-32)./9`. This conversion is used to determine temperature changes in thermal expansion calculations, such as finding the change in area of an aluminum plate when temperature changes, applying the coefficient of linear expansion .

The coefficient of thermal expansion quantifies how much a material's dimensions change with temperature. It is crucial for designing structures subject to temperature variations, ensuring that expansions do not compromise structural integrity. Different materials have varying coefficients, influencing application considerations in engineering and construction .

A temperature conversion function modifies input temperatures from Fahrenheit to Celsius, facilitating precise thermal expansion calculations using the expansion coefficient. For instance, using the conversion to find a temperature differential, engineers calculate resulting dimensional changes in construction materials like metals, essential for design considerations and structural integrity .

Benefits include streamlined computation by applying operations across an entire vector, enhancing performance through vectorization, and reducing loop usage. Limitations may include complexity managing large data sets or memory constraints, necessitating efficient implementation to ensure resource optimization while balancing accuracy .

Modifying the search path enables MATLAB to locate and access user-defined function files stored in custom directories, enhancing workflow efficiency by organizing and managing code libraries, allowing seamless function calls from any location without file relocation, crucial for structured project management .

To transform a function for vectorized input, element-wise operations are defined using dot notation (e.g., `.^`, `.*`). This applies functions across vector elements simultaneously, crucial for computational efficiency as it minimizes loop use, expedites calculations, and optimizes performance on large datasets .

Script files execute a series of commands in sequence without function definition or parameterization, impacting all variables in the workspace. In contrast, function files define reusable custom functions with specific inputs and outputs, encapsulating functionality and preventing workspace variable interference, making them modular and versatile for repeated tasks .

The user-defined function for calculating when x is a vector is written as `function y=chp7one(x)` with `y=(x.^4.*sqrt(3*x+5))./(x.^2+1).^2`, allowing element-by-element calculations. To use it, create a vector for x, pass it as an argument to the function, and the output y will also be a vector, corresponding to each element of x .

Inaccurate temperature measurements or assumptions can lead to significant errors in calculated expansions, risking structural integrity or function. Variability in material properties or uniformity under thermal effects may further complicate accuracy, leading to over- or underestimation of dimensional changes, affecting safety and performance .

You might also like