Defining and calling functions in MATLAB

355 ビュー (過去 30 日間)
Spaceman
Spaceman 2024 年 2 月 27 日
コメント済み: Spaceman 2024 年 3 月 2 日
Given: Recall that Fahrenheit and Celsius are related by the following equation:
Find: Create a function called tempC2F that will accept an input argument of a single temperature in degrees Celsius and return the equivalent temperature in degrees Fahrenheit. I should be able to call your function using the following function call: TF=tempC2F(TC)
My Solution: This is what I have, but I am not getting the desired result, I'm not sure how to call the function to run...
function
Invalid expression. Check for missing or extra characters.
tempC2F=(1.8*tC)+32
end
% You can test your function using the following function call. Recall that 212F is the boiling point of water at sea level.
% This corresponds to 100C. If you click "Run", the following code will call your function. You should get an output of tF=212.
% Remember that we are "Calling" the function below. As such, the variable names do not have to match the variables used in the function above.
% Those variables are specific to the function itself.
tC = 100;
tF = tempC2F(tC)

採用された回答

Voss
Voss 2024 年 2 月 27 日
Refer to the following documentation page for a description and examples of how to define and call a function:
  5 件のコメント
Steven Lord
Steven Lord 2024 年 2 月 28 日
I think this has to do with the fact that I have to name the function itself 'tempC2F'... Even though it states in my practice example that the variable names do not have to match the variables used in the function.
These are two different things.
In general, the name of the main function in a function file should have the same name as the file. If they don't match, you will need to use the name of the file to call the function, not the name of the first function. So if you wrote the code function tF = tempC2F(tC) in a file named myTemperatureConverter.m you'd need to call it as tF = myTemperatureConverter(100) rather than tF = tempC2F(100).
The second sentence in the part of your message that I quoted deals with variables, not functions. In general (leaving out things like nested functions or global variables) a function call's workspace is completely independent of what's outside the workspace. If your function defines a variable named x, for example, that's not going to affect or be affected by a variable named x in the workspace in which you call your function. The main ways that data flows into or out of a function are through the input and output arguments.
X = pi
X = 3.1416
callFunctionThatUsesXInternally(42)
X % This didn't change even when the function assigned 42 to X inside its workspace
X = 3.1416
function callFunctionThatUsesXInternally(value)
X = value; % This is independent of the variable on the 1st line of code segment
end
Spaceman
Spaceman 2024 年 2 月 29 日
編集済み: Spaceman 2024 年 3 月 2 日
Thank you, my lord. This clears things up. I think the main takeaway here is that a function call's workspace is completely independent of what's outside the workspace.

サインインしてコメントする。

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2024 年 2 月 27 日
Create a function called tempC2F
function tempC2F
that will accept an input argument of a single temperature in degrees Celsius
function tempC2f(single_temperature_in_degrees_Celcius)
and return the equivalent temperature in degrees Fahrenheit.
function equivelent_temperature_in_degrees_Fahrenheit = tempC2f(single_temperature_in_degrees_Celcius)
  6 件のコメント
Steven Lord
Steven Lord 2024 年 2 月 29 日
I didn't know it was so obvious I am so new
\begin{Yoda mode}
When for more than 20 years you have been answering questions about MATLAB, novice users will you be able to spot :)
\end{Yoda mode}
Spaceman
Spaceman 2024 年 3 月 2 日
Hilarious as you are wise XD

サインインしてコメントする。


Aquatris
Aquatris 2024 年 2 月 27 日
So if you want to define them in a single script, the function needs to be at the bottom.
And as others mentioned you might want to check out the documentation on how to define functions. But here is your code arrange to work.
tC = 100;
tF = tempC2F(tC)
tF = 212
function tFahr = tempC2F(tCelcius)
tFahr=(1.8*tCelcius)+32;
end
  3 件のコメント
Aquatris
Aquatris 2024 年 2 月 28 日
Just to make sure there are no confusion, here are a couple things that might be helpful to your understanding.
  • The function name is 'tempC2F'
  • The function output is 'tFahr'
  • 'tFahr' variable only lives within the function. Outside of the function, nobody knows what 'tFahr' is (there might be a completely different variable with the same name and that's why you might want to checkout the topic of 'workspaces' here).
  • The syntax matlab uses to define a function is literally how you would use that function from another piece of code with a 'function' attached to the front to define it as a function.
% function definition
function functionOutput = myFunctionName(functionInput)
% whatever the function needs to do,
% in this case it just multiplies the input by 5 and returns that value
functionOutput = 5*functionInput;
end
And to call the function from outside as:
x = myFunctionName(10); % this will make x = 50
  • To call the function, the script that holds the function needs to be in your matlab path. Because it was not in your matlab path you got the error 'Unrecognized function or variable 'tempC2F' 'in previous comments.
Spaceman
Spaceman 2024 年 3 月 2 日
This. I actually finally learned about functions and this explains it perfectly. Thank you. I assume when it gets more complicated with loops and such I will be right back here asking questions, lol.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品


リリース

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by