Reputation: 11
I am trying to solve following functions
k=2;
G(1)=292000.0;
G(2)=262000.0;
Ld(1)=0.00396;
Ld(2)=0.0344;
deps=10;
aa=3.7;
ms=0.0;
for i=1:k
ms=@(x) ms+(G(i)/Ld(i))*exp(-x./Ld(i))
end
f=@(x) (exp(x.*2*deps)-exp(-x.*deps))/((aa-3)+(2*exp(x.*deps)+exp(-2*x.*deps)))
g=@(x) ms(x).*f(x)
g(1);
but I receive this error "Undefined function or method 'plus' for input arguments of type 'function_handle'."
hope somebody can help me out..Thanks
Upvotes: 1
Views: 7221
Reputation: 74940
The problematic lines are:
ms=0.0;
for i=1:k
ms=@(x) ms+(G(i)/Ld(i))*exp(-x./Ld(i))
end
Inside the loop, you treat ms
as both a function handle as well as a number, which won't work.
Although recursive definition of a function handle is most likely not the best way to go, it is - to my surprise - possible. Thus, you can write:
ms = @(x)0; %# initialize 'ms' to nothing
for i=1:k
ms = @(x) ms(x) +(G(i)/Ld(i))*exp(-x./Ld(i));
end
Upvotes: 1
Reputation: 125874
As Jonas already pointed out, the problem is that you treat ms
interchangeably as a numeric value and a function handle, which you can't do.
You actually don't need the for loop to generate the anonymous function ms
. You can create it in one line using the function SUM like so:
ms = @(x) sum((G./Ld).*exp(-x./Ld));
This will give you a final result of g(1) = 0.0199;
.
Upvotes: 2