send function to a function
1 view (last 30 days)
Show older comments
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 Comments
Answers (3)
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');
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,
- you don't close the parentheses in your function definition.
- you call midpoint() when the function is called midpoints()
3 Comments
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.
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;
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 Comments
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.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!