send function to a function

5 visualizzazioni (ultimi 30 giorni)
shay
shay il 25 Lug 2014
Commentato: Patrik Ek il 29 Lug 2014
hey this is my function that i build :
function [ answer ] = midpoints(f,a,b,error)
t=0.0001;
f1=diff(f,x);
f2=diff(f1,x);
maxf2=max(abs(double(subs(f2,a:t:b))));
n=0;
while (b-a)^2/(24*(n)^2)*(b-a)*maxf2>=error
n=n+1;
end
h=(b-a)/n;
answer=0;
for i=1:n
answer=answer+f((a+((2*i-1)*h)/2));
end
disp(answer);
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
  5 Commenti
Patrik Ek
Patrik Ek il 25 Lug 2014
Modificato: Patrik Ek il 25 Lug 2014
Try to have x as an input as well (which must be a sym).
shay
shay il 25 Lug 2014
and how do i do that?

Accedi per commentare.

Risposte (3)

Michael Haderlein
Michael Haderlein il 25 Lug 2014
Modificato: Michael Haderlein il 25 Lug 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 Commento
shay
shay il 27 Lug 2014
if i open a script and want to use the function midpoints i write :
f=@(x) x+2;
midpoints(f,0,1,0.0001)
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

Accedi per commentare.


Wayne King
Wayne King il 25 Lug 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()
  3 Commenti
shay
shay il 27 Lug 2014
this is the midpoint integrate that i do,
this is correct and working well i try this on any function integrate and its working great,i tell you i have only problem with the sending the function to a function, when i try to send this
f=@(x) x+2;
midpoints(f,0,1,0.0001)
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
Patrik Ek
Patrik Ek il 28 Lug 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.

Accedi per commentare.


Patrik Ek
Patrik Ek il 28 Lug 2014
Modificato: Patrik Ek il 28 Lug 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;
midpointse(f,1,2,0.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).
  2 Commenti
shay
shay il 28 Lug 2014
first of all,thank you for the all information.
and second, if i have a symbolic function can i send her points to calculate, like : f(x)=x+1 and if i do f(1)+f(2) i will get 5,because in my function(midpoints) i need to send points to the function f(x).
so i think i need to use as "anonymous function", so tell me how can i send any function f(x) and to calculate it with my function midpoints. what i need to write in the script for send f(x) or pass her by reference.to midpoints.
or also what i need to change in midpoints function to get this work
thank
Patrik Ek
Patrik Ek il 29 Lug 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.

Accedi per commentare.

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by