send function to a function

2 ビュー (過去 30 日間)
shay
shay 2014 年 7 月 25 日
コメント済み: Patrik Ek 2014 年 7 月 29 日
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 件のコメント
Patrik Ek
Patrik Ek 2014 年 7 月 25 日
編集済み: Patrik Ek 2014 年 7 月 25 日
Try to have x as an input as well (which must be a sym).
shay
shay 2014 年 7 月 25 日
and how do i do that?

サインインしてコメントする。

回答 (3 件)

Michael Haderlein
Michael Haderlein 2014 年 7 月 25 日
編集済み: Michael Haderlein 2014 年 7 月 25 日
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 件のコメント
shay
shay 2014 年 7 月 27 日
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

サインインしてコメントする。


Wayne King
Wayne King 2014 年 7 月 25 日
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 件のコメント
shay
shay 2014 年 7 月 27 日
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 2014 年 7 月 28 日
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.

サインインしてコメントする。


Patrik Ek
Patrik Ek 2014 年 7 月 28 日
編集済み: Patrik Ek 2014 年 7 月 28 日
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 件のコメント
shay
shay 2014 年 7 月 28 日
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 2014 年 7 月 29 日
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.

サインインしてコメントする。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by