I'm trying to solve an implicit matrix equation with fsolve

I'm able to solve an implicit equation with fsolve:
function y = brillouin(x,p)
A=p(1); B=p(2);
y=zeros(size(x));
NN=length(x);
opt=optimset('display','off');
for i=1:NN
y(i)=fsolve(@(y) y-tanh(A.*x(i)-B*y), 1e-9, opt);
end
end
I'm now trying to expand this to a vector y, with B being a matrix. Any help would be appreciated! Thanks.

 Accepted Answer

Matt J
Matt J on 10 Jun 2013
Edited: Matt J on 10 Jun 2013
yet, and that is the problem, that the solutions, y(i,x) from fsolve are identical for each of the i=1:6.
That's because you are calling FSOLVE with a scalar initial point 1e-9. FSOLVE is therefore returning scalars.
If FSOLVE is supposed to be returning something in R^6, you must feed it an initial point that is in R^6.

More Answers (3)

Your code should work as is, even if A and B are matrices instead of scalars.

4 Comments

Oren Commented:
Thanks! alas, as the function is written now, y is a vector already, with a length the size of x. When i try to set B as a matrix, the multiplication B.*y is erroneous since y is a matrix the size of (size(b),size(x)).
Couldn't understand any of that. Please execute at the command line
>> whos A B x
in the case where A and B are matrix data, so that we can see how big everything is. Then tell us how many equations/unknowns will be processed in a single call to FSOLVE in this case.
Yes. My apologies, it works well: not producing what i would expected but the code does work. Thanks Matt J! I'm getting: "Warning: Trust-region-dogleg algorithm of FSOLVE cannot handle non-square systems; using Levenberg-Marquardt algorithm instead. "
any ideas why ?
Because you have more or fewer equations than unknowns.

Sign in to comment.

Oren
Oren on 9 Jun 2013
Edited: Matt J on 9 Jun 2013
Relocated to Comment by Matt J
Here is the problem: my function is y(x)=tanh(h*x-m*y) where m is a 6x6 matrix, h is 6x1 vector and x is a vector i define as x=0:0.1:2.5 . the solution for y=y(x) should be 6x length(x) (which in the case here is 6x26).
in order for the code to run, i use h=repmat(h,1,6); so that h and m have the same size (6x6).
the matrix m is defined as
m=[0 0 2 0 2 0; 0 0 0 2 0 2; 2 0 0 0 2 0; 0 2 0 0 0 2; 2 0 2 0 0 0; 0 2 0 2 0 0];
the vector h is
h=[0 1 0 1 0 1];
yet, and that is the problem, that the solutions, y(i,x) from fsolve are identical for each of the i=1:6. the code i use is the same as the above except:
y=zeros(6,NN);
h=repmat(h,1,6);
for i=1:NN
y(:,i)=fsolve(@(y) y-tanh(x(i)*h-m*y), 1e-9, opt);
end
Thank you!

Categories

Find more on Mathematics in Help Center and File Exchange

Products

Asked:

on 9 Jun 2013

Community Treasure Hunt

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

Start Hunting!