Help creating a function

7 次查看(过去 30 天)
I've done the tutorials and searched the forums and I cannot find an answer to my question. I want to define a series of functions that I can call on using the function and its associated values. Like this:
F(x,y,z)=x*y*z -this is a dumbed down version of what I need F(1,2,3)= 6 -this is the result I'm hoping to get so that I can use the function for various scenarios.
That way I can define a particular case z=f(1,2,3) z=6

采纳的回答

Star Strider
Star Strider 2018-2-4
I would create them as Anonymous Functions (link).
Example
F = @(x,y,z) x*y*z;
q = F(1,2,3)
q =
6
  3 个评论
Kevin Edlebeck
Kevin Edlebeck 2018-2-4
ok, so I did it the way you suggested and it work, would you mind giving me a quick FYI about how this works. Just lame terms, I'm not incredible program savy. Does this work with an array?
Star Strider
Star Strider 2018-2-4
I cannot go into detail about how MATLAB handles functions work because I have not inquired into it. (I once knew how FORTRAN created the stack interfaces and such, since I had to write assembly language functions and had to know how to make them compatible with the FORTRAN compiler.)
When you create a function, you give it a list of inputs (arguments) to use within the function to calculate the result you want. See Function Basics (link) for details. They are designed to work essentially as algebraic functions work.
Note that anonymous functions will use variables from your user workspace unless you declare those variables with the same names as your workspace variables to be function arguments. Then it uses the variables passed to it as arguments instead.
Functions work with arrays if you write them to do so. See the documentation on Array vs. Matrix Operations (link) and Vectorization (link) for details.
I am not avoiding answering your questions about functions. The MATLAB documentation discusses them much more clearly and efficiently than I can, so I direct you to it.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Fortran with MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by