Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Taking initial guess for fsolve

Subject: Taking initial guess for fsolve

From: kalpana

Date: 29 Aug, 2013 06:59:05

Message: 1 of 9

I have two non-linear functions f1(x,y) = 0 & f2(x,y) = 0.
I have to find the solution of f1(x,y) = 0 & f2(x,y) = 0.

For this, i tried to use solve(f1,f2) first. But, it was taking very long time to get the solution (x & y values).

So, i have to use "fsolve" now which requires initial guess for the x & y values.
To get the Initial guess for fsolve function, i am taking some x & y values that gives min. value for f1 & f2.
I am using the following code :
        syms x y f1 f2
        f1(x,y) % calculation of f1 function
        f2(x,y) % calculation of f2 function
         X = -2 : 0.25 : 55; Y = -2 : 0.25 : 55;
         f1val = subs(f1, {x,y}, {X, Y}) ;
         f2val = subs(f2, {x,y}, {X, Y}) ;
         fsolve_ind1 = find( abs(f1val ) == min( abs(f1val ) ) );
         fsolve_ind2 = find( abs(f2val ) == min( abs(f2val ) ) );
         fh = matlabFunction([f1 ; f2],'vars',{[x,y]});
        options = optimset('Display','off','TolFun',1e-10);
         [sol_fsolve(:,1), value(:,1), outflag(1)] = fsolve(fh, [X(fsolve_ind1), Y(fsolve_ind1) ] ,options );
         [sol_fsolve(:,2), value(:,2), outflag(2)] = fsolve(fh, [X(fsolve_ind2), Y(fsolve_ind2) ] ,options );

I am taking one of the o/ps ( i.e. sol_fsolve ) as x & y values that has lesser value (i.e. min. of value(:,1) and value(:,2) ).

But, i am getting the final x & y values as : 8.9830e+000 & 1.7937e+001 where as the original x & y values that i should get are : 8.7616e+000 & 2.0232e+001.

That means there is difference in the x & y values that i am supposed to get. This indicates that i am not making the initial guess properly. Can anyone tell me how to make the initial guess for x & y values ? ( with out plotting ezsurf because plotting the surfaces takes long time ).

Please please suggest some thing on how to make the initial guess for fsolve ? Am i doing some thing wrong in the above code ?

please do give some suggestion.

Thank you for your time.

With best regards,
kalpana

Subject: Taking initial guess for fsolve

From: Torsten

Date: 29 Aug, 2013 07:35:08

Message: 2 of 9

"kalpana " <naidukalpana@yahoo.co.in> wrote in message <kvmrfp$kbn$1@newscl01ah.mathworks.com>...
> I have two non-linear functions f1(x,y) = 0 & f2(x,y) = 0.
> I have to find the solution of f1(x,y) = 0 & f2(x,y) = 0.
>
> For this, i tried to use solve(f1,f2) first. But, it was taking very long time to get the solution (x & y values).
>
> So, i have to use "fsolve" now which requires initial guess for the x & y values.
> To get the Initial guess for fsolve function, i am taking some x & y values that gives min. value for f1 & f2.
> I am using the following code :
> syms x y f1 f2
> f1(x,y) % calculation of f1 function
> f2(x,y) % calculation of f2 function
> X = -2 : 0.25 : 55; Y = -2 : 0.25 : 55;
> f1val = subs(f1, {x,y}, {X, Y}) ;
> f2val = subs(f2, {x,y}, {X, Y}) ;
> fsolve_ind1 = find( abs(f1val ) == min( abs(f1val ) ) );
> fsolve_ind2 = find( abs(f2val ) == min( abs(f2val ) ) );
> fh = matlabFunction([f1 ; f2],'vars',{[x,y]});
> options = optimset('Display','off','TolFun',1e-10);
> [sol_fsolve(:,1), value(:,1), outflag(1)] = fsolve(fh, [X(fsolve_ind1), Y(fsolve_ind1) ] ,options );
> [sol_fsolve(:,2), value(:,2), outflag(2)] = fsolve(fh, [X(fsolve_ind2), Y(fsolve_ind2) ] ,options );
>
> I am taking one of the o/ps ( i.e. sol_fsolve ) as x & y values that has lesser value (i.e. min. of value(:,1) and value(:,2) ).
>
> But, i am getting the final x & y values as : 8.9830e+000 & 1.7937e+001 where as the original x & y values that i should get are : 8.7616e+000 & 2.0232e+001.
>
> That means there is difference in the x & y values that i am supposed to get. This indicates that i am not making the initial guess properly. Can anyone tell me how to make the initial guess for x & y values ? ( with out plotting ezsurf because plotting the surfaces takes long time ).
>
> Please please suggest some thing on how to make the initial guess for fsolve ? Am i doing some thing wrong in the above code ?
>
> please do give some suggestion.
>
> Thank you for your time.
>
> With best regards,
> kalpana

If your system of agebraic equations has multiple zeros, then, depending on your
initial guess, fsolve will produce different solutions.
Did you check whether both "solutions" satisfy f(x,y)=g(x,y)=0 ?
If this is the case, there is little chance to adjust the initial guess such that the solver converges to the solution that you qualify as "correct".
If this is not the case, you will have to find out why this is so.

Best wishes
Torsten.

Subject: Taking initial guess for fsolve

From: kalpana

Date: 29 Aug, 2013 09:41:08

Message: 3 of 9


For X = -2 : 0.2 : 1000 & Y = -2 : 0.2 :1000, i have calculated f1(X,Y) & f2(X,Y) values.
I checked the min(f1) and min(f2) values. They are coming around 10^(-4) & 10^(-6). But, still the initial guess taken is coming as wrong. May be, i have to further increase the X range and Y range for calculating f1(X,Y) & f2(X,Y) values . This is something like : X = -2 : 0.02 : 1000 & Y = -2 : 0.02 :1000.

When i take like this, some times i am getting correct values. But, some times, i am getting as : " Out of memory. HELP MEMORY for your options " . I have to look for some other way to circumwent this problem.

Thank you for your answer.

With best regards,
kalpana

Subject: Taking initial guess for fsolve

From: Torsten

Date: 29 Aug, 2013 10:02:11

Message: 4 of 9

"kalpana " <naidukalpana@yahoo.co.in> wrote in message <kvn4vj$imv$1@newscl01ah.mathworks.com>...
>
> For X = -2 : 0.2 : 1000 & Y = -2 : 0.2 :1000, i have calculated f1(X,Y) & f2(X,Y) values.
> I checked the min(f1) and min(f2) values. They are coming around 10^(-4) & 10^(-6). But, still the initial guess taken is coming as wrong. May be, i have to further increase the X range and Y range for calculating f1(X,Y) & f2(X,Y) values . This is something like : X = -2 : 0.02 : 1000 & Y = -2 : 0.02 :1000.
>
> When i take like this, some times i am getting correct values. But, some times, i am getting as : " Out of memory. HELP MEMORY for your options " . I have to look for some other way to circumwent this problem.
>
> Thank you for your answer.
>
> With best regards,
> kalpana

What is the result when you evaluate
f1(8.9830e+000,1.7937e+001), f2( 8.9830e+000,1.7937e+001)
and
f1(8.7616e+000,2.0232e+001), f2(8.7616e+000,2.0232e+001)
?

Best wishes
Torsten.

Subject: Taking initial guess for fsolve

From: Steven_Lord

Date: 29 Aug, 2013 13:57:03

Message: 5 of 9



"Torsten " <Torsten.Hennig@umsicht.fraunhofer.de> wrote in message
news:kvn673$72j$1@newscl01ah.mathworks.com...
> "kalpana " <naidukalpana@yahoo.co.in> wrote in message
> <kvn4vj$imv$1@newscl01ah.mathworks.com>...
>>
>> For X = -2 : 0.2 : 1000 & Y = -2 : 0.2 :1000, i have calculated f1(X,Y) &
>> f2(X,Y) values. I checked the min(f1) and min(f2) values. They are coming
>> around 10^(-4) & 10^(-6). But, still the initial guess taken is coming as
>> wrong. May be, i have to further increase the X range and Y range for
>> calculating f1(X,Y) & f2(X,Y) values . This is something like : X = -2 :
>> 0.02 : 1000 & Y = -2 : 0.02 :1000. When i take like this, some times i am
>> getting correct values. But, some times, i am getting as : " Out of
>> memory. HELP MEMORY for your options " . I have to look for some other
>> way to circumwent this problem. Thank you for your answer.
>>
>> With best regards,
>> kalpana
>
> What is the result when you evaluate
> f1(8.9830e+000,1.7937e+001), f2( 8.9830e+000,1.7937e+001) and
> f1(8.7616e+000,2.0232e+001), f2(8.7616e+000,2.0232e+001)
> ?

That's the wrong question to ask. The correct question is, when you run your
code like:

solution = fsolve(...)

what is the result you receive when you execute:

functionValues = [f1(solution(1), solution(2)), f2(solution(1),
solution(2))]

Sometimes the digits that don't print using FORMAT SHORT can be significant.
Using the contents of the variables instead of the displayed values of the
variables avoids that problem.

To the OP, if that doesn't resolve the problem, post a SMALL description of
your f1 and f2 functions. If they're "well behaved" then you may just need
to relax your tolerance. Instead of using a tolerance of 1e-10 on the
function value, try more like 1e-6 or 1e-7. If your function is something
oscillatory or "poorly behaved" then you may have problems. For instance,
trying to find a solution to this:

http://www.mathworks.com/matlabcentral/fileexchange/35311-matlab-plot-gallery-surface-contour-plot/content/html/Surface_Contour_Plot.html

is probably going to lead you in circles (literally.)

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Taking initial guess for fsolve

From: kalpana

Date: 30 Aug, 2013 08:46:10

Message: 6 of 9


Thank you very much sirs for giving replies.
Functions are f1(x,y) = 0 & f2(x,y) = 0.

I am giving code so that you can evaluate the code on your system directly.

syms x y f1 f2
f1 = 2822464228751971/(4503599627370496*((2484360419406893*x)/36028797018963968 + (5582909719129191*y)/9007199254740992 + 1)) - 140234579115081/(4503599627370496*((8089325470800733*x)/144115188075855872 + (3304637458891429*y)/288230376151711744 + 1)) + 320816161903291/(2251799813685248*((6024484531985885*x)/9007199254740992 + (6197466521865093*y)/36028797018963968 + 1)) + 1041325558631747/(36028797018963968*((1388000383401105*x)/576460752303423488 + (603386941836381*y)/9007199254740992 + 1)) + 3248847174498757/(36028797018963968*((2338212506704719*x)/288230376151711744 + (8910002399985347*y)/72057594037927936 + 1)) + 2996237640680017/(36028797018963968*((3416882408443869*x)/2251799813685248 + (8442790123351649*y)/72057594037927936 + 1)) + 2276131449401325/(4503599627370496*((2248687015757317*x)/576460752303423488 + (4572443574873573*y)/9007199254740992 + 1)) +
4479020945402813/(72057594037927936*((6524521880384857*x)/9007199254740992 + (1760797610596235*y)/18014398509481984 + 1)) + 1470675427754771/(18014398509481984*((8050589761579725*x)/9007199254740992 + (8341274636311469*y)/72057594037927936 + 1)) - 4826228655060733/(144115188075855872*((7699484597058077*x)/4611686018427387904 + (334769527150077*y)/36028797018963968 + 1)) ;

f2 = 1242486569177609/(72057594037927936*((2484360419406893*x)/36028797018963968 + (5582909719129191*y)/9007199254740992 + 1)) + 121629086934271/(36028797018963968*((8089325470800733*x)/144115188075855872 + (3304637458891429*y)/288230376151711744 + 1)) + 5998260655906273/(9007199254740992*((6024484531985885*x)/9007199254740992 + (6197466521865093*y)/36028797018963968 + 1)) - 1971407955810865/(36028797018963968*((1388000383401105*x)/576460752303423488 + (603386941836381*y)/9007199254740992 + 1)) - 874581120276129/(18014398509481984*((2338212506704719*x)/288230376151711744 + (8910002399985347*y)/72057594037927936 + 1)) + 7131526150318003/(4503599627370496*((3416882408443869*x)/2251799813685248 + (8442790123351649*y)/72057594037927936 + 1)) - 3826478189007699/(72057594037927936*((2248687015757317*x)/576460752303423488 + (4572443574873573*y)/9007199254740992 + 1)) +
6538975049719411/(9007199254740992*((6524521880384857*x)/9007199254740992 + (1760797610596235*y)/18014398509481984 + 1)) + 8189185522877099/(9007199254740992*((8050589761579725*x)/9007199254740992 + (8341274636311469*y)/72057594037927936 + 1)) - 500042358655175/(9007199254740992*((7699484597058077*x)/4611686018427387904 + (334769527150077*y)/36028797018963968 + 1)) ;

fh = matlabFunction([f1 ; f2],'vars',{[x,y]});
options = optimset('Display','off','TolFun',1e-10);
min_rise = 0.02;
X = -0.2 : min_rise: 1500; Y = -0.2 : min_rise : 1500;
f1val = subs(f1, {x,y}, {X, Y}) ;
f2val = subs(f2, {x,y}, {X, Y}) ;
fsolve_ind1 = find( abs(f1val ) == min( abs(f1val ) ) );
fsolve_ind2 = find( abs(f2val ) == min( abs(f2val ) ) );
[min(f1val), min(f2val)]
if( ( min( abs(f1val ) ) <= 10^(-6) ) && ( min( abs(f2val ) ) <= 10^(-6) ) )
          fsolve_ind1 = find( abs(f1val ) == min( abs(f1val ) ) );
          fsolve_ind2 = find( abs(f2val ) == min( abs(f2val ) ) );
elseif( ( min( abs(f1val ) ) > 10^(-6) ) || ( min( abs(f2val ) ) > 10^(-6) ) )
          floor_f1val = log10(abs(f1val ));
          expone_f1 = fix(floor_f1val );
          floor_f2val = log10(abs(f2val));
          expone_f2 = fix(floor_f2val );
          divide_min = abs( -6/max( min( expone_f1 ), min(expone_f2) ) );
          if( divide_min >= 6 )
                   divide_min = 3;
          end
          min_rise = min_rise/(10^divide_min);
          X = -0.2 : min_rise: 1500; Y = -0.2 : min_rise : 1500;
          clear f1val ; clear f2val ;
          f1val = subs(f1, {x,y}, {X, Y}) ;
          f2val = subs(f2, {x,y}, {X, Y}) ;
          fsolve_ind1 = find( abs(f1val ) == min( abs(f1val ) ) );
          fsolve_ind2 = find( abs(f2val ) == min( abs(f2val ) ) );
          [min(f1val), min(f2val)]
end
%%%%%% Taking the surrounding X & Y values that bring min values for f1 &
%%%%%% f2 functions.
x_value = [X(fsolve_ind1), X(fsolve_ind1), X(fsolve_ind1), X(fsolve_ind1) + 1, X(fsolve_ind1) - 1, X(fsolve_ind1) + 2, X(fsolve_ind1) -2 ,X(fsolve_ind1) , X(fsolve_ind1) ];
y_value = [Y(fsolve_ind1) + 1, Y(fsolve_ind1)-1, Y(fsolve_ind1), Y(fsolve_ind1), Y(fsolve_ind1) , Y(fsolve_ind1), Y(fsolve_ind1), Y(fsolve_ind1) + 2, Y(fsolve_ind1) -2 ];
x1_value = [X(fsolve_ind2), X(fsolve_ind2), X(fsolve_ind2), X(fsolve_ind2) + 1, X(fsolve_ind2) - 1, X(fsolve_ind2) + 2, X(fsolve_ind2) -2 ,X(fsolve_ind2) , X(fsolve_ind2) ];
y1_value = [Y(fsolve_ind2)+1, Y(fsolve_ind2)-1,Y(fsolve_ind2), Y(fsolve_ind2), Y(fsolve_ind2) , Y(fsolve_ind2), Y(fsolve_ind2), Y(fsolve_ind2) + 2, Y(fsolve_ind2) -2 ];

%%%% using fsolve to get the solution for f1 & f2 functions with initial guess as %%%%% x_value & y_value
for i = 1 : length(x_value),
        [sol_fsolve(:,i), value(:,i), outflag(i)] = fsolve(fh, [x_value(i), y_value(i) ] ,options );
end

%%%% using fsolve to get the solution for f1 & f2 functions with initial guess as %%%%% x1_value & y1_value

later_xvalue = length(x_value) + 1;
for i = 1 : length(x1_value),
        [sol_fsolve(:,later_xvalue), value(:,later_xvalue), outflag(later_xvalue)] = fsolve(fh, [x1_value(i), y1_value(i) ] ,options );
        later_xvalue = later_xvalue + 1 ;
end
%%% Trying to findout the solution for f1 & f2 functions by taking those values which %%% give lesser " value" for the functions.

len_pos = length( sol_fsolve );
if( len_pos ~= 0)
    indpos1 = find( (sol_fsolve(1,:)>=0)&(sol_fsolve(2,:)>=0));
    if( length(indpos1) >= 1)
        op_val = find( abs(value(1,indpos1)) == min( abs(value(1,indpos1)) ) );
        op_val1 = find( abs(value(2,indpos1)) == min( abs(value(2,indpos1)) ) );
        if( op_val == op_val1 )
               if( length(op_val) > 1 )
                   final_x_value= sol_fsolve(1, indpos1(op_val(1)) ); final_y_value= sol_fsolve(2, indpos1(op_val(1)) );
               elseif(length(op_val) == 1 )
                      final_x_value= sol_fsolve(1, indpos1(op_val) ); final_y_value = sol_fsolve(2, indpos1(op_val) );
               end
        elseif( abs( abs(value(1,op_val)) - abs(value(1,op_val1)) ) < abs( abs(value(2,op_val)) - abs(value(2,op_val1)) ) )
                 if( length(op_val1)> 1)
                     final_x_value= sol_fsolve(1, indpos1(op_val1(1)) ); final_y_value= sol_fsolve(2, indpos1(op_val1(1)) );
                 elseif(length(op_val1) == 1 )
                          final_x_value = sol_fsolve(1, indpos1(op_val1) ); final_y_value= sol_fsolve(2, indpos1(op_val1) );
                 end
        elseif( abs( abs(value(1,op_val)) - abs(value(1,op_val1)) ) >= abs(abs(value(2,op_val)) - abs(value(2,op_val1))) )
                       if( length(op_val)> 1)
                          final_x_value= sol_fsolve(1, indpos1(op_val(1)) ); final_y_value= sol_fsolve(2, indpos1(op_val(1)) );
                        elseif(length(op_val) == 1 )
                             final_x_value= sol_fsolve(1, indpos1(op_val) ); final_y_value= sol_fsolve(2, indpos1(op_val) );
                        end
        end
    else
          final_x_value= 0; final_y_value= 0;
    end
else
      final_x_value= 0; final_y_value= 0;
end

[final_x_value,final_y_value]


The correct values to be obtained for final_x_value & final_y_value are : 4.4552e+002 & 8.4597e+002 . But, i am getting final_x_value & final_y_value as : 4.3948e+002 & 8.3229e+002.
If i give command " [op, val] = fsolve(fh, [ 4.4552e+002, 8.4597e+002 ] ,options ) " for the correct x & y values , then i get the f1 function value = 2.9363e-009 & f2 function value = -3.6558e-008.

For the command " [op, val] = fsolve(fh, [ 4.3948e+002 , 8.3229e+002 ] ,options ) " , then i get the f1 function value = 6.6746e-006 & f2 function value = -4.0965e-006.

These f1 & f2 function values clearly tell me that i am missing out the initial guess for fsolve command.

If you know, please tell me how to make the correct initial guess for fsolve .

Sorry if it is consuming your time and thank you very much again for spending your time for my problem.

With best regards,
kalpana




"Steven_Lord" <slord@mathworks.com> wrote in message <kvnjve$efk$1@newscl01ah.mathworks.com>...
>
>
> "Torsten " <Torsten.Hennig@umsicht.fraunhofer.de> wrote in message
> news:kvn673$72j$1@newscl01ah.mathworks.com...
> > "kalpana " <naidukalpana@yahoo.co.in> wrote in message
> > <kvn4vj$imv$1@newscl01ah.mathworks.com>...
> >>
> >> For X = -2 : 0.2 : 1000 & Y = -2 : 0.2 :1000, i have calculated f1(X,Y) &
> >> f2(X,Y) values. I checked the min(f1) and min(f2) values. They are coming
> >> around 10^(-4) & 10^(-6). But, still the initial guess taken is coming as
> >> wrong. May be, i have to further increase the X range and Y range for
> >> calculating f1(X,Y) & f2(X,Y) values . This is something like : X = -2 :
> >> 0.02 : 1000 & Y = -2 : 0.02 :1000. When i take like this, some times i am
> >> getting correct values. But, some times, i am getting as : " Out of
> >> memory. HELP MEMORY for your options " . I have to look for some other
> >> way to circumwent this problem. Thank you for your answer.
> >>
> >> With best regards,
> >> kalpana
> >
> > What is the result when you evaluate
> > f1(8.9830e+000,1.7937e+001), f2( 8.9830e+000,1.7937e+001) and
> > f1(8.7616e+000,2.0232e+001), f2(8.7616e+000,2.0232e+001)
> > ?
>
> That's the wrong question to ask. The correct question is, when you run your
> code like:
>
> solution = fsolve(...)
>
> what is the result you receive when you execute:
>
> functionValues = [f1(solution(1), solution(2)), f2(solution(1),
> solution(2))]
>
> Sometimes the digits that don't print using FORMAT SHORT can be significant.
> Using the contents of the variables instead of the displayed values of the
> variables avoids that problem.
>
> To the OP, if that doesn't resolve the problem, post a SMALL description of
> your f1 and f2 functions. If they're "well behaved" then you may just need
> to relax your tolerance. Instead of using a tolerance of 1e-10 on the
> function value, try more like 1e-6 or 1e-7. If your function is something
> oscillatory or "poorly behaved" then you may have problems. For instance,
> trying to find a solution to this:
>
> http://www.mathworks.com/matlabcentral/fileexchange/35311-matlab-plot-gallery-surface-contour-plot/content/html/Surface_Contour_Plot.html
>
> is probably going to lead you in circles (literally.)
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Subject: Taking initial guess for fsolve

From: kalpana

Date: 30 Aug, 2013 11:15:08

Message: 7 of 9

Just now, i checked the following code for getting solution to non-linear functions f1(x,y) = 0 & f2(x,y) = 0.

fh = matlabFunction([f1 ; f2],'vars',{[x,y]});
options = optimset('Display','off','TolFun',1e-10);
tstart = tic;
grid = linspace(-2, 800, 25);
[xx, yy] = meshgrid(grid, grid);
f1val = subs(f1, {x,y}, {xx, yy}) ;
f2val = subs(f2, {x,y}, {xx, yy}) ;
C1 = contour(xx, yy, f1val, [0, 0]);
ind_C1 = find( ( C1(1,:) >= 0) & ( C1(2,:) >= 0) );
pos_C1 = C(:,ind_C);
C2 = contour(xx, yy, f2val, [0, 0]);
ind_C2 = find( ( C2(1,:) >= 0) & ( C2(2,:) >= 0) );
pos_C2 = C2(:,ind_C2);
for i = 1 : length(A1),
        [sol(:,i), val(:,i), out(i)] = fsolve(fh, [A1(1,i), A1(2,i) ] ,options );
end
later_i = length(A1) + 1;
for i = 1 : length(B1),
    [sol(:,later_i), val(:,later_i), out(later_i)] = fsolve(fh, [B1(1,i), B1(2,i) ] ,options );
    later_i = later_i + 1;
end
tend = toc( tstart )

I got correct solution. But, this code is taking relatively long time when compared to guessing the initial value for fsolve and then using the fsolve function.

Is there any way to reduce the time taken for solving the f1 and f2 functions using the contour method ? ( I have to check if i get the correct solution for other problems also using the contour method and how much time it takes for solving).

Thank you for whatever suggestions i got until now.
Also, Please suggest on how we can reduce the time taken for solving the equations f1(x,y) = 0 and f2(x,y) = 0.

Thank you very very much.

With best regards,
kalpana

Please

Subject: Taking initial guess for fsolve

From: Torsten

Date: 30 Aug, 2013 12:27:10

Message: 8 of 9

"kalpana " <naidukalpana@yahoo.co.in> wrote in message <kvpurs$pti$1@newscl01ah.mathworks.com>...
> Just now, i checked the following code for getting solution to non-linear functions f1(x,y) = 0 & f2(x,y) = 0.
>
> fh = matlabFunction([f1 ; f2],'vars',{[x,y]});
> options = optimset('Display','off','TolFun',1e-10);
> tstart = tic;
> grid = linspace(-2, 800, 25);
> [xx, yy] = meshgrid(grid, grid);
> f1val = subs(f1, {x,y}, {xx, yy}) ;
> f2val = subs(f2, {x,y}, {xx, yy}) ;
> C1 = contour(xx, yy, f1val, [0, 0]);
> ind_C1 = find( ( C1(1,:) >= 0) & ( C1(2,:) >= 0) );
> pos_C1 = C(:,ind_C);
> C2 = contour(xx, yy, f2val, [0, 0]);
> ind_C2 = find( ( C2(1,:) >= 0) & ( C2(2,:) >= 0) );
> pos_C2 = C2(:,ind_C2);
> for i = 1 : length(A1),
> [sol(:,i), val(:,i), out(i)] = fsolve(fh, [A1(1,i), A1(2,i) ] ,options );
> end
> later_i = length(A1) + 1;
> for i = 1 : length(B1),
> [sol(:,later_i), val(:,later_i), out(later_i)] = fsolve(fh, [B1(1,i), B1(2,i) ] ,options );
> later_i = later_i + 1;
> end
> tend = toc( tstart )
>
> I got correct solution. But, this code is taking relatively long time when compared to guessing the initial value for fsolve and then using the fsolve function.
>
> Is there any way to reduce the time taken for solving the f1 and f2 functions using the contour method ? ( I have to check if i get the correct solution for other problems also using the contour method and how much time it takes for solving).
>
> Thank you for whatever suggestions i got until now.
> Also, Please suggest on how we can reduce the time taken for solving the equations f1(x,y) = 0 and f2(x,y) = 0.
>
> Thank you very very much.
>
> With best regards,
> kalpana
>
> Please

When I give a relative precision of eps=1.5e-8 for my fsolve-like solver and start with initial guess x=1, y=1, I automatically arrive at the correct solution
x=445.518284, y=845.973029.
So I can't give advice what's going wrong here.

Best wishes
Torsten.

Subject: Taking initial guess for fsolve

From: kalpana

Date: 30 Aug, 2013 15:10:14

Message: 9 of 9

My problem was that it takes long time to guess the initial value for fsolve.

B ut, now that you have given the idea , i tried the following code :

 options = optimset('Display','off','TolFun',eps);
>> [op,val] = fsolve(fh, [ 1,1] ,options )

Yes. It gave the correct result as : 445.5183 & 845.9731

Thank you very much for your advice (taking eps as the tolerance ). I hope it works for other problems also.

Thank you very much for your advice.

With best regards,
kalpana

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us