Solving simultaneous equations numerically

I have two equations that need to be solved numerically, I can't write out the equations explicitly but they are both of the form:
x = \sum_{i=1}^N (a-(b_i)x)/((a-(b_i)x)^2+q^2y^2)
All the parameters other than x and y are fixed in this equation. Both my equations are of similar forms but I'm not sure what is the best way to solve this numerically in MATLAB as these equations involve the summation and I have never dealt with this kind of equation in MATLAB before.
p.s. sorry for the mess but I hope the form of the equation is clear enough. Thanks.

 Accepted Answer

Here is what you need to do
1) write a function like this:
funX=@(x,y,N,a,q) sum((a-b.*x)./((a-b.*x).^2+q.^2.*y.^2));
funY=@(x,y,N,a,q) whatever the equation is for Y
fun1=@(x,y,N,a,q) ([funX(x,y,N,a,q); ...
funY(x,y,N,a,q)]);
Note that fun1 is the right-hand-side of your equation. (correct it if I miss code it)
2) use "fsolve" or another method of your choice to solve it as follow
a= 'the value you want'
N= 'the value you want'
q= 'the value you want'
%Your first equation is x=FunX based on your question.
%Assuming your second equation is also of the form y=FunY
%P=[x;y] so P(1)=x and P(2)=y;
fun2=@(P) (P-fun1(P(1),P(2),N,a,q))
InitialGuess=[x0;y0];
fsolve(fun2,InitialGuess)
This way, you can change N, a, q programitically

20 Comments

Thanks, I'll give this a go and see if it works for me.
John Smith
John Smith on 17 Oct 2014
Edited: John Smith on 17 Oct 2014
I've given this method a go and I have a few questions if you don't mind. First of all are the '@(x,y,N,a,q)' necessary? I have tried both with and without and can't get the code working yet but it seems to be better without.
I'm not certain if I'm representing the summation correctly but I use symsum in MATLAB right?
symsum is for the symbolic toolbox. If you have the numbers, you should use sum
'@(x,y,N,a,q)' is not necessary. You can hard code N,a,q into the equation, but anytime that you want to change N,a,q you have to edit your equation. (x,y) is the minimum required. but including N,a,q gives more flexibility in changing them.
John Smith
John Smith on 17 Oct 2014
Edited: John Smith on 17 Oct 2014
Thanks I will try now with sum and see if it work.
I seem to have the sum working now and have put the code in but MATLAB still gives me an error. To try and understand this error correctly what does the line:
fun2=@(P) (P-fun1(P(1),P(2),N,a,q)) actually mean?
I sort of get that you put the terms of the equation to one side so it's now set to 0 and solving it from thereon. What's @(P)? It looks like you put both the equations in a 2x1 matrix but why is (P(1),P(2),N,a,q) needed in here? Thanks again!
so you have two equations: to solve
x=FunX
y=FunY
so you want to solve for both x and y. your two equations become
x-FunX(x,y,...)=0
y-FunY(x,y,...)=0
if we write this in matrix form we can have
P-fun2(x,y,...)=0
where:
x
p=
y
and
funX
fun2=
funY
so P(1) is x and P(2) is y. Therefor to call fun2(x,y,...) we do fun2(P(1),P(2), ...)
Once you pass this to fsolve, it changes the value of P, i.e. changes the value of x and y until it gets a value that satisfies both of the equation.
You also need to read on
to get better understanding of
f=@(P) (some equation using P)
Thanks, I'll give this another go and see how I get on.
Ok as some of my parameters are non-scalar(vectors) I have used cell arrays {} instead of [] as MATLAB didn't allow me to use arrays for non-scalars. I have also put P as a cell array P={x;y}; to keep it consistent with the other cell arrays. MATLAB however doesn't seem to recognise this as it keeps telling me this error:
Undefined function or variable 'x'.
Error in * (line 8)
P ={x;y};
I thought x and y were already defined as variables in the earlier functions? i.e. fun1 and fun2.
Should this work if some of the parameters are not scalars? Just want to clarify this as I don't want to get stuck in a dead end. The b_i's in this case are the N eigenvalues that are assigned to it and in MATLAB this is set as a column vector so b = [x1; x2; x3; ...;xN] as an example. Where x1, x2 etc are the eigenvalues.
Yes, it should work.
If you wanted upload a sample a,b,n and also both of your equation I can give it a try.
John Smith
John Smith on 20 Oct 2014
Edited: John Smith on 20 Oct 2014
Here are the equations with N=100 a=1 and I have attached the MATLAB file that generates the eigenvalues for the equation. It is given by e in the MATLAB file. I have also attached the code for the function and a separate file to solve the equation to show my attempt if it's anyway near close to finish. If there is anything that isn't clear please let me know. Thanks! EDIT: Oh I forgot that the eigenvalues were given by a row vector and not a column vector but I don't think it matters that much anyway.
These are strange equations look at the following (starting with the second equation)
Are you sure these are proper equations? Based on these, the value of x and y is not linked to the value of b_i
John Smith
John Smith on 20 Oct 2014
Edited: John Smith on 21 Oct 2014
It's for a particular case if you want the whole equation without the parameters being set then the link to the equation can be found here:
Now in this paper the variables are all labelled differently but it's the same equation. Apologies for the confusion with the variables changed. I just took the simplest case of lambda = 0 to try and solve as I need to do this for a wide range of cases with varying lambda but I can handle that part, the most important part is being able to solve the equation in the first place. My x and y are t and q respectively. b_i = v_i in this case. Thanks again.
I see.
Ok, for Lamba=0 the finding is that t and q are not dependent on v_i or b_i. :D That is a finding by itself.
I look into it for non-zeroLambda then.
Some thing like this should work
Fun1=@(t,q,v,lambda) [ t-mean((v.*(lambda-v*t))./((lambda-v*t).^2+(q*v).^2)); ...
1-mean(v.^2./((lambda-v*t).^2+(q*v).^2))];
% Set your Lambda and v before Fun2;
% It doesn't need to be here but it should be before Fun2
v=rand(100,1);
lambda=0;
Fun2=@(P) (Fun1(P(1),P(2),v,lambda));
P0=[0.1, 0.9]; % initial guesses for t and q
[P,fval]=fsolve(Fun2,P0);
disp(['t = ' num2str(P(1))])
disp(['q = ' num2str(P(2))])
Here is the MATLAB output running above code
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>
t = 3.5763e-09
q = 1
As we show, once Lambda=0, x(or t)= 0 and q(or y)= +-1
If this satisfies your question please accept it as the answer.
Thank you very much for your help. As I do not have access to MATLAB at the moment, I need to test it tomorrow to see if it works for the sum when lambda =/= 0. If it works then I will accept it straight away.
Just tried it and it works perfectly, thanks!

Sign in to comment.

More Answers (1)

It doesn't matter that your two equations involve an extended summation. You can still create a function that generates these sums which you can use with the Optimization Toolbox function, 'fsolve'. You will, however, need to also furnish a guess or estimate to start the search for a solution. See:
http://www.mathworks.com/help/optim/ug/fsolve.html

5 Comments

John Smith
John Smith on 16 Oct 2014
Edited: John Smith on 16 Oct 2014
Thanks for your reply. Would this still work without entering values for N, a and q? Even though these parameters are fixed I still need to do it in general rather than fixing it to a certain number.
Then it is not a numerical solution if those other variables are unknown! You are looking for a symbolic solution. Numerical solvers are not capable of solving symbolic problems.
Ok it came out a bit wrong there and I apologise for the confusion. They are not unknowns I just need to know how the answer varies as I vary these parameters. So I can in fact set them to any value to solve but I need the ability to change these values at any time
No. I understood exactly what you were asking. It did not come out wrong.
This means they are unknowns, symbolic variables. You are asking for a symbolic solution. Just because something is an unknown does not mean that you need to solve for it, or that you can solve for it!
A numerical solver (fsolve, lsqnonlin, fmincon, or fzero for a few examples) CANNOT have unknown symbolic variables in the problem. It is no longer a numerical problem, but a symbolic one, not in their domain.
If you really want to see how the solution varies as a function of these variables, the best you can do with a numerical solver is to substitute in some values for those parameters, THEN you can solve the problem using a numerical solver, and I suppose plot the result as a function of those parameters you have substituted.
If however, you insist on seeing an analytical solution in terms of those unknowns, then you need to use a tool that can handle symbolic computations.
There is no mistake here. I do understand what you are asking.
Substituting numbers in and plotting is fine, my problem is only trying to get it to work in the first place with numbers substituted in.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!