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

2 views (last 30 days)
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)

Matt J
Matt J on 9 Jun 2013
Your code should work as is, even if A and B are matrices instead of scalars.
  4 Comments
Oren
Oren on 10 Jun 2013
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 ?
Matt J
Matt J on 10 Jun 2013
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

Oren
Oren on 10 Jun 2013
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

Community Treasure Hunt

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

Start Hunting!