fsolve with three anonymous functions
조회 수: 14 (최근 30일)
이전 댓글 표시
Hello everybody,
I can't see why giving a cell array of three separate anonymous as input to fsolve() doesn't work:
a = 6;
f = @(x)[sin(x);x;x*x];
g = @(x) sin(x);
h = @(x) x;
y1 = fsolve(f,a)
y2 = fsolve({g,f},a)
y3 = fsolve({f,g,h},a)
y1 and y2 will be calculated, y3 results in an error:
??? Error using ==> lsqfcnchk at 117
FUN must be a function or an inline object;
What do I miss over here? or, FUN may be a cell array that contains these type of objects.
댓글 수: 0
채택된 답변
Andrew Newell
2011년 3월 29일
O.k., Walter thinks we haven't answered your original question, and he may be right. As his comment on your answer shows, the loop can work - but not in the form you have displayed it. If you define a function this way,
function EqSys = makeSystem(N)
EqSys = cell(1,N);
for i=1:N
EqSys{i} = @(x)x^i;
end
and then try these commands
>> EqSys = makeSystem(2)
EqSys =
@(x)(x-i)^i @(x)(x-i)^i
the output is a cell array of function handles that works as in Walter's comment - for example,
>> EqSys{2}(1)
ans =
1
Then if you type
>> fsolve(EqSys,1)
you get an answer of 0 (preceded by a lot of diagnostic statements).
But did you really get the simultaneous solution of these equations? If your function is
function EqSys = makeSystem(N)
EqSys = cell(1,N);
for i=1:N
EqSys{i} = @(x)(x-i)^i;
end
and you enter
>> EqSys = makeSystem(2);
>> fsolve(EqSys,1)
you will get an answer of 1 (which only solves the first equation).
I don't see any way of setting this up using function handles that is worth the trouble. Instead, you could create a function like this:
function y = eqnSystem(x,N)
y = zeros(N,1);
for i=1:N
y(i) = (x(i)-i)^i;
end
which, by the way, assumes that x has as many components as there are equations - a requirement for a well-defined problem. Then
>> x0 = [2 3];
>> f = @(x) eqnSystem(x,N);
>> fsolve(f,x0)
gives the answer
1.0000 2.0075
which is surprisingly inaccurate (but you can improve this by adjusting the options for fsolve).
댓글 수: 3
Andrew Newell
2011년 3월 29일
I'm not sure what you mean. If I call EqSys = makeSystem(2), I get
EqSys =
@(x)(x-i)^i @(x)(x-i)^i
which behaves just as you described in your comment.
Walter Roberson
2011년 3월 29일
Ah, I hadn't noticed the function definition... sorry, I was in a rush then.
추가 답변 (4개)
Andrew Newell
2011년 3월 23일
The full message is
??? Error using ==> lsqfcnchk at 117
FUN must be a function or an inline object;
or, FUN may be a cell array that contains these type of objects.
The third statement is not the whole truth. I looked at the code, and lsqfcnchk only allows cell arrays of length 1 or 2. However, the documentation for fsolve does not claim that you can input cell arrays at all. So the documentation is not quite right.
Of course, you can still solve each equation separately.
댓글 수: 0
Simon
2011년 3월 29일
댓글 수: 1
Walter Roberson
2011년 3월 29일
Your loop does work
>> EqSys{2}(5)
ans =
25
>> EqSys{4}(5)
ans =
625
The text representation of the function handles _does_ appear to have ^i in it, suggestive of it not having worked, but each of those "i" had its value "captured" at the time of function creation.
참고 항목
카테고리
Help Center 및 File Exchange에서 Function Creation에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!