s2=@(n,f,phi0) ((n+1-i)/(1-i))*phi0^(1-i)*(f^(1-i)-1);
Now I want to sum up s2 over i=2:n and want to get a resultant sum function as z=@(n,f,phi0) sum(s2(2:n,f,phi0)). Because, I need to call z in several places of my program with different inputs. Can anybody please help me to resolve the problem? I have tried with cellfun, funarray commands following several topic discussed in the matlab forum but none of them really worked!
Looking forward to your response(s),
Regards,
Kanjilal
"Suranita " <kanj...@geophysik.uni-frankfurt.de> wrote in message
news:iicc2e$ou9$1...@fred.mathworks.com...
> Hi,
> I have faced a function handle stated below:
>
> s2=@(n,f,phi0) ((n+1-i)/(1-i))*phi0^(1-i)*(f^(1-i)-1);
Unless i was defined before this function call, MATLAB will treat your use
of it as a call to the built-in function.
http://www.mathworks.com/help/techdoc/ref/i.html
If it was defined before, that value will be "memorized' by the function
handle s2 and you will NOT be able to change it without redefining s2, as it
is not an input argument for this anonymous function.
> Now I want to sum up s2 over i=2:n and want to get a resultant sum
> function as z=@(n,f,phi0) sum(s2(2:n,f,phi0)). Because, I need to call z
> in several places of my program with different inputs. Can anybody please
> help me to resolve the problem? I have tried with cellfun, funarray
> commands following several topic discussed in the matlab forum but none
> of them really worked!
You cannot add function handles.
You CAN add the values obtained by _evaluating_ function handles, however.
--
Steve Lord
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com
why not use z in the form that you've already written it above. I.e., by defining
z=@(n,f,phi0) sum(s2(2:n,f,phi0)).
you can now make function calls of the form z(n,f,phi0) to obtain the n-term sum.
Well, you should consider investigating that, because I'm having absolutely no difficulty when I do it:
>> i=3; s2=@(n,f,phi0) ((n+1-i)/(1-i))*phi0^(1-i)*(f^(1-i)-1);
>> z=@(n,f,phi0) sum(s2(2:n,f,phi0));
>> z(3,4,2)
ans =
0.1172
"Matt J" wrote in message <iid3cm$o1r$1...@fred.mathworks.com>...
If you get a different answer, it's most likely because the value of i
is different from the one used in the evaluation above. You've got a
hint earlier in this thread about what might be wrong -- the value of
i seen by s2 is the one i had at the time s2 was defined, not the one
i might have at the time s2 is called:
i = 0; foo = @() i;
foo()
% 0
i = 1; foo()
% 0
If i in your formula for s2 is supposed to be fixed (you do want it to
be a constant throughout all calls), then you may want to use that
constant value to avoid confusion. However, if you expect i to be
different from call to call, you need to include it in the function's
parameters. As you can see above, the approach of setting i
externally to s2 does not work. Try redefining s2 (and possibly z) as
s2 = @(n, f, phi0, i) ...
and see what happens.
vQ
I'm seeing absolutely no difference between the computation that I showed you and the same computation done manually.
My guess is the same as waku and Steve about why you're not getting what you expect.
"Matt J" wrote in message <iiei13$gk$1...@fred.mathworks.com>...
Do you know how to use the command SUM?
sum_i = 2:n [ sin(i) ]
can be written in Matlab as
>> n = 10
>> sum(sin(2:n))
Bruno