Optimizing my nonlinear system of equations solving code

2 ビュー (過去 30 日間)
jerome
jerome 2014 年 11 月 9 日
回答済み: jerome 2014 年 11 月 10 日
Hi,
I wrote some code to solve some non linear system of equation.
The number of equation vary (I want the highest number as possible for precision)
First I did it for 10 equation explicitly, then I did it for i equations and my code is working until i is around 20 to 25.
close all
clear all
clc
%declare number of type
ntype=25
%I declare the function I will solve
t=sym('type',[1 ntype]);
w=sym('wack',[1 ntype]);
fct=sym('n',[1 ntype]);
fct2=sym('m',[ntype ntype]);
% I create my type between 0 and 1.
type = 1:ntype;
type = type/ntype;
n = length(type)
type = type';
dm1 = diag(ones(1,n));
dm2 = diag(ones(1,n-1),-1);
dm3 = diag(ones(1,n-1),1);
matchset = dm1+dm2+dm3;
type=sort(type,'descend');
prod =type*type';
matchnom = t*matchset;
for ii = 1:ntype
t(ii)-(0.05*(1/n)/((0.05+5*(matchnom(ii)))))
fct(ii)=ans
end
assume(t>0);
v1=vpasolve(fct,t);
The 6 last line are, I define all my functions and place them in a vector with a loop and then on the last line I solve them.
My program continue after that and I use vpasolve on a more complicated non lin system, but i have no problem to solve that one. This solve is exponentielly longer , for 10 eq its 2 sec, 15 eq 6 sec , 20 eq 6 min , 22 eq 12 min, 25 eq it bugs and give me these warnings, and the values are not calculated:
Warning: Solutions might be lost. [solvelib::allvalues_warning]
Warning: Could not extract individual solutions. Returning a MuPAD set object.
> In sym.vpasolve>assignOutputs at 145
In sym.vpasolve at 118
I think vpasolve compute alot of solution but i need the only one where every variable>0, when i was doing it explicitly it gave me 12 answer for 10 type and 72 for 25 type, meaning 12 solution for the 10 variable versus 72 solution for 25 variable. So as the numb of equation/variables increase, the number of solution increase alot.
vpasolve seems to give me too many answer, I only want the answer where every variable is >0 and there will be only one set of value like that.
I'm fairly new, my code might seem fairly bad because I made it all by myself by looking at many answer on forums . It is working but not at the degree I would like to.
Any help would be greatly appreciated.
Jerôme

回答 (1 件)

jerome
jerome 2014 年 11 月 10 日
Hi,
using fsolve do the trick but I use a vector of equation. If i do it by hand its something like:
function fcns=eqns(z)
a=z(1);
b=z(2);
c=z(3);
d=z(4);
e=z(5);
f=z(6);
g=z(7);
h=z(8);
i=z(9);
j=z(10);
k=z(11);
l=z(12);
m=z(13);
o=z(14);
p=z(15);
q=z(16);
r=z(17);
s=z(18);
t=z(19);
u=z(20);
v=z(21);
w=z(22);
x=z(23);
y=z(24);
aa=z(25);
fcns(1) = a-(0.05*(1/10)/((0.05+5*(a+b))));
fcns(2) = b-(0.05*(1/10)/((0.05+5*(a+b+c))));
fcns(3) = c-(0.05*(1/10)/((0.05+5*(b+c+d))));
fcns(4) = d-(0.05*(1/10)/((0.05+5*(c+d+e))));
fcns(5) = e-(0.05*(1/10)/((0.05+5*(d+e+f))));
fcns(6) = f-(0.05*(1/10)/((0.05+5*(e+f+g))));
fcns(7) = g-(0.05*(1/10)/((0.05+5*(f+g+h))));
fcns(8) = h-(0.05*(1/10)/((0.05+5*(g+h+i))));
fcns(9) = i-(0.05*(1/10)/((0.05+5*(h+i+j))));
fcns(10) = j-(0.05*(1/10)/((0.05+5*(i+j+k))));
fcns(11) = k-(0.05*(1/10)/((0.05+5*(j+k+l))));
fcns(12) = l-(0.05*(1/10)/((0.05+5*(k+l+m))));
fcns(13) = m-(0.05*(1/10)/((0.05+5*(l+m+o))));
fcns(14) = o-(0.05*(1/10)/((0.05+5*(m+o+p))));
fcns(15) = p-(0.05*(1/10)/((0.05+5*(o+p+q))));
fcns(16) = q-(0.05*(1/10)/((0.05+5*(p+q+r))));
fcns(17) = r-(0.05*(1/10)/((0.05+5*(q+r+s))));
fcns(18) = s-(0.05*(1/10)/((0.05+5*(r+s+t))));
fcns(19) = t-(0.05*(1/10)/((0.05+5*(s+t+u))));
fcns(20) = u-(0.05*(1/10)/((0.05+5*(t+u+v))));
fcns(21) = v-(0.05*(1/10)/((0.05+5*(u+v+w))));
fcns(22) = w-(0.05*(1/10)/((0.05+5*(v+w+x))));
fcns(23) = x-(0.05*(1/10)/((0.05+5*(w+x+y))));
fcns(24) = y-(0.05*(1/10)/((0.05+5*(x+y+aa))));
fcns(25) = aa-(0.05*(1/10)/((0.05+5*(y+aa))));
end
And then I use:
guess=[ones(25,1)];
result=fsolve(@eqns, guess)
It solve in 2 sec. My only problem now is how to use a vector of equation (sym) inside the function because i generate my equations using this code:
for ii = 1:ntype ;
t(ii)-(0.05*(1/n)/((0.05+5*(matchnom(ii)))));
fct(ii)=ans;
end;
so simply how to use a vector of equation and not type in all the equation inside the function argument.
Thanks

カテゴリ

Help Center および File ExchangeNumeric Solvers についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by