fsolve with multidimensional array and x0

8 vues (au cours des 30 derniers jours)
Sung-Ryul Huh
Sung-Ryul Huh le 14 Juin 2012
Hello, everyone.
Here is a question. I've tried to solve some nonlinear equations (not simultaneous equations) of multi-dimensional array form by R2012a fsolve and then seen error message.
the following is the simplified code for this question.
b(:,:,1)=[2 3;4 6;1 9];b(:,:,2)=2.*[2 3;4 6;1 9];
and the error messages are...
Error using .* Matrix dimensions must agree.
Error in @(x)x.^3+b.*x+8
Error in C:\Program Files\MATLAB\R2012a\toolbox\shared\optimlib\finDiffEvalAndChkErr.p>finDiffEvalAndChkErr (line 26)
Error in C:\Program Files\MATLAB\R2012a\toolbox\shared\optimlib\finitedifferences.p>finitedifferences (line 128)
Error in trustnleqn (line 97) [JACfindiff,~,~,numFDfevals] = finitedifferences(x,funfcn{3},[],[],[],Fvec,[],[], ...
Error in fsolve (line 397) [x,FVAL,JACOB,EXITFLAG,OUTPUT,msgData]=...
plz, let me know where the problems are.
best regards
S. R. Huh

Réponse acceptée

Sargondjani le 15 Juin 2012
if you want to stick more to your orginal formulation: the problem is that your x0 has to be a vector, not an array, so you will have to reshape both x (and b). also f has to return a vector. so: x1=reshape(x,[],1) and the same with b, and the resulting f will also be vector
  1 commentaire
Sargondjani le 18 Juin 2012
I guess you are working together with jinny zhou, but i should have mentioned that you should set the 'JacobPattern' to speye(length(x)). because all equations are independent, you should tell matlab that it doesn not need to estimate the full jacobian matrix

Connectez-vous pour commenter.

Plus de réponses (1)

Andrei Bobrov
Andrei Bobrov le 14 Juin 2012
for your case: EDIT
b(:,:,1)=[2 3;4 6;1 9];b(:,:,2)=2.*[2 3;4 6;1 9];
a = [1 0 0 8];
b1 = zeros(numel(b(:)),numel(a));
b1(:,3) = b(:);
c = bsxfun(@plus,a,b1);
rt = cell2mat(arrayfun(@(x)roots(c(x,:)),(1:numel(b))','un',0));
out = reshape(rt,[numel(a)-1,1,size(b)]);
  1 commentaire
Sung-Ryul Huh
Sung-Ryul Huh le 18 Juin 2012
It's also helpful answer. Thanks a lot, andrei.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by