2

Suppose we have a function defined as:

function(f, df, x0)

where f is a function, df is its derivative, and x0 is an initial point. How do we defined f on the command line? Do you use an inline definition? What about df and x0? What if df is a gradient? Also if x0 is an ordered pair, how do you define it in the command line?

1 Answer 1

1

To pass a function as a variable, you need to use a function handle. A simple way to demonstrate this is to use a function handle to an anonymous function. A simple anonymous function can be defined as follows:

handle = @(arglist)anonymous_function

So, to make an anonymous function that adds 2 numbers, you could do something like the following:

f = @(a,b)a+b;

You can use this like any other function

>> f(1,2)

ans =

     3

If df is just a simple numeric value, it can be defined as follows:

df = 0.4

To define a pair of values, you could do it like so:

X0=[1 2]

Finally, you can put it all together with this example function (put this in a file called myfunc) . . .

function out = myfunc(f,df,x0)

out = df * f(x0(1), x0(end));

Is this what you want? I was slightly confused by "x0 is an ordered pair".

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.