send function to a function

2 views (last 30 days)
shay on 25 Jul 2014
Commented: Patrik Ek on 29 Jul 2014
hey this is my function that i build :
function [ answer ] = midpoints(f,a,b,error)
while (b-a)^2/(24*(n)^2)*(b-a)*maxf2>=error
for i=1:n
when i try to use the function i write : midpoint(x+2,0,1,0.0001) and i get error.
i want to send any input function F,to the "midpoints" and let her calculate and return the answer.
what should i do?
thanks shay
shay on 25 Jul 2014
and how do i do that?

Sign in to comment.

Answers (3)

Michael Haderlein
Michael Haderlein on 25 Jul 2014
Edited: Michael Haderlein on 25 Jul 2014
Could you please tell us where the error occurs and what the error message says? Also, it looks as if the first line of the function is incomplete. Finally, I suppose that f and x are symbolic variables, is that right?
After having a closer look, I think x is not defined in your function workspace. Either use it as parameter (such as f, a, b, error) or define it inside (syms x).
Edit: Or use the string option: f1=diff(f,'x');
  1 Comment
shay on 27 Jul 2014
if i open a script and want to use the function midpoints i write :
f=@(x) x+2;
and the error i get is
"??? Undefined function or method 'midpoints' for input arguments of type 'function_handle'."
this is the biggest issue that i try to understand

Sign in to comment.

Wayne King
Wayne King on 25 Jul 2014
You should always report the errors you get, that is very helpful in allowing people to help you.
I see a number of immediate issues here and I'm not sure which ones are just typos in your post.
For example,
  1. you don't close the parentheses in your function definition.
  2. you call midpoint() when the function is called midpoints()
Patrik Ek
Patrik Ek on 28 Jul 2014
Ok I see, you try to use the midpoint rule for numerical intgration. The name of the function gave the impression that you wanted to find the midpoint of an arc. However, I still stick to that you need to have x as an input variable. A more thorough example will be posted as an answer.

Sign in to comment.

Patrik Ek
Patrik Ek on 28 Jul 2014
Edited: Patrik Ek on 28 Jul 2014
The error you have is most likely a simple function call. I tried to reproduce your error, but the only way to do that was to call a function with the wrong name, for example
f = @(x) 2*x+1;
Undefined function 'midpointse' for input arguments of type 'function_handle'.
To solve that problem, try to copy the function midpoints to a new file, delete the old file and then save the new file. The thing is that a matlab function (those created by typing function in the beginning), needs to be saved in a file with the same name. Then make sure to call the function with correct syntax.
However, your problems may not end there. There are a few different types of functions in matlab. The one I choose to call a matlab function is one type.
An other way to do it is as an "anonymous function". The call is f = @(x) a*x+b+1. When this definition is made then a and b must be set. This function works in large the same way a matlab function except that you do not need an .m-file. this does also mean that if you want a anonymous function to return a vector, the you need to define the function as f = @(x) a.*x+b+1. More important you cannot use symbolic differentiation diff(f,x) when you have an anonymous function. The variable x (or rather all variables declared within the @()) for an anonymous function is, just like any matlab function only defined in the local scope. That means also that a function handle is unable to pass a variable to a function. You have probably mixed this up with a "symbolic function", which may look similar at a first glance.
An symbolic function is special. That function creates a "symbolic expression" Which may consist of multiple symbols, syms. However these variables need to be defined before the symbolic expression is written. A sym exactly work as any other types in matlab in the sense that unless defined global (which you probably do not want to do with x, since you may have x in a lot of functions) the variable will not be defined inside midpoints. Also, writing syms x is really only a short way for writing x = sym('x'), which means that a variable named x is assigned a value x which is of the type sym.
Looking at this it is clear the the preferred function for you is the symbolic function. To be able to differentiate h with respect to x, the best way is to pass symbols as variables (so that the variable can have another value than x and not cause problems).
function [f1,out] = symFunExample(f,a,x)
f1 = diff(f,x); out = subs(f,x,1); out = out+a;
This function is then called as: syms x; f = 2*x+2; a = 1; symFunExample(f,a,x).
Patrik Ek
Patrik Ek on 29 Jul 2014
You could use an anonymous function with a symbolic variable. Write a function:
function [fsum,f1,out] = symFunExample(f,a,x)
f1 = diff(f,x);
out = subs(f,x,1);
out = out+a;
fsum = f(2)+f(2);
Then try
syms x;
f = @(x) x^2+1;
[fsum,f1,out] = symFunExample(f,1,x);
It seems as if diff works here as well. However, notice that the differentiated variable f1 will be of class sym. Still, you can always use subs, it is just a little more clumsy way of writing it. It could also be possible to use eval to automatically create symbolic expressions, but that route leads to hell. The code is quite short anyway so subs should work fine.

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!