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

Sample Problem 7

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)
31 views2 pages

Sample Problem 7

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=[Link]
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([Link])
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

You might also like