fsolve is really confusing me. All the examples find show a system of equations being input in character form. Mine are in matrix form. As I understand it x0 is meant to define a boundary condition for solving the system, so that the solver doesn't fly off into infinity and crash if some of the equations are bad. I don't understand the point of function f though. Shouldn't this be the array which contains the system of equations to be solved?
I've seen several examples of fsolve in matlab, but can't seem to find any that show how to pass parameters in matrix form.
In this example I'm using only two equations but in practice I actually have hundreds to solve simultaneously so it's a rather large matrix.
Thanks!
Here is my code.
[A,b] = equationsToMatrix(eq1,eq2)
X0 = [0 0]
fsolve([A,b], X0)
Here is the output
eq1 = - sx - sy/2 == 5
eq2 = - (3*sx)/2 - (3*sy)/2 == 9
A =
[ -1, -1/2]
[ -3/2, -3/2]
b =
5
9
X0 =
0 0
Error using lsqfcnchk (line 109)
If FUN is a MATLAB object, it must have an feval method.
Error in fsolve (line 198)
funfcn = lsqfcnchk(FUN,'fsolve',length(varargin),funValCheck,gradflag);
Error in SolveTesting (line 70)
fsolve([A,b], X0)

 채택된 답변

Yatin
Yatin 2013년 10월 18일

0 개 추천

Hi,
I see that your equations are linear. You can therefore use matrix left division ("\") for better speed and accuracy instead of " fsolve ". So as per the above case:
A = [-1, -1/2; -3/2,-3/2];
b = [5;9];|
then
x = A\b;
This should give you your desired result.

댓글 수: 6

Drew
Drew 2013년 10월 18일
I appreciate the response but this isn't what I'm looking for. I realize that the example I'm giving is linear, but in practice I'm dealing with equations that aren't. The point of my question is to figure out the proper syntax to use fsolve.
Yatin
Yatin 2013년 10월 18일
Hi,
I think this tutorial from youtube will be really helpful as it explains with an example.
Drew
Drew 2013년 10월 18일
Following this video here is what I've tried and it's not working.
I created the following function
function [f] = matrixfun(A,b)
syms x y;
f = A * [x;y] - b;
end
I then run this code:
>> matrixfun(A,b)
ans =
- x - y/2 - 5
- (3*x)/2 - (3*y)/2 - 9
>> syms guess
>> guess = [3 3];
>> fsolve(matrixfun(A,b),guess)
Error using lsqfcnchk (line 109)
If FUN is a MATLAB object, it must have an feval method.
Error in fsolve (line 198)
funfcn = lsqfcnchk(FUN,'fsolve',length(varargin),funValCheck,gradflag);
Drew
Drew 2013년 10월 18일
I realized that my function isn't returning an evaluated answer. So I've made the following code changes.
fsolve(@(z)A*[z(1);z(2)]-b,[-5 -5])
And to test this code I do the following:
>> test = @(z)A*[z(1);z(2)]-b
test =
@(z)A*[z(1);z(2)]-b
>> test([-5 -5])
ans =
5/2
6
>> test([-4 -2])
ans =
0
0
So I know my function is good. However when I run fsolve I get the following:
>> fsolve(@(z)A*[z(1);z(2)]-b,[-5 -5])
Error using fsolve (line 257)
FSOLVE requires all values returned by user functions to be of data type double.
Drew
Drew 2013년 10월 18일
For good measure I have also tried the following.
function f = matrixfun(z,A,b)
%syms x y;
f = A * [z(1);z(2)] - b;
end
Same problem
>> fsolve(@matrixfun,[-4 -5],[],A,b)
Error using fsolve (line 257)
FSOLVE requires all values returned by user functions to be of data type double.
btw, I love the utterly useless error messages that matlab provides.
Drew
Drew 2013년 10월 18일
Solved it, ahhhhhh!
>> fsolve(@(z)double(A)*[z(1);z(2)]-double(b),[-5 -5])
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
ans =
-4.0000 -2.0000

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Systems of Nonlinear Equations에 대해 자세히 알아보기

태그

질문:

2013년 10월 18일

댓글:

2013년 10월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by