Help creating a function

6 visualizaciones (últimos 30 días)
Kevin Edlebeck
Kevin Edlebeck el 4 de Feb. de 2018
Comentada: Star Strider el 4 de Feb. de 2018
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

Respuesta aceptada

Star Strider
Star Strider el 4 de Feb. de 2018
I would create them as Anonymous Functions (link).
Example
F = @(x,y,z) x*y*z;
q = F(1,2,3)
q =
6
  3 comentarios
Kevin Edlebeck
Kevin Edlebeck el 4 de Feb. de 2018
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 el 4 de Feb. de 2018
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Fortran with MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by