How can I troubleshoot problems with fsolve in MATLAB?

  • Context: MATLAB 
  • Thread starter Thread starter La82
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting issues with the fsolve function in MATLAB, particularly focusing on syntax errors, function definitions, and the behavior of the solver when no solution is found. The scope includes technical explanations and problem-solving strategies related to numerical methods in programming.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • Lonnie describes an issue with fsolve, indicating that the function lambda_y1 is reported as unknown, and provides a function definition that includes multiple equations.
  • One participant asks for clarification on the unknowns Lonnie is trying to find.
  • Another participant points out that the syntax used by Lonnie is incorrect and suggests a simpler syntax for fsolve, emphasizing the need to initialize unknowns within the function.
  • Lonnie confirms that the suggested changes resolved the initial issue but encounters a new problem where fsolve indicates that no solution is found, suggesting potential issues with the equations themselves.
  • A later reply suggests checking the equations with known values and simplifying them to debug the problem, while also noting the possibility of no solution existing.

Areas of Agreement / Disagreement

Participants generally agree on the need to correct the syntax and initialization of variables, but there remains uncertainty regarding the validity of the equations and whether a solution exists.

Contextual Notes

There are unresolved aspects regarding the specific equations being used, their correctness, and the conditions under which fsolve may fail to find a solution.

La82
Messages
5
Reaction score
0
Hi everyone,

I am a MATLAB newbie and am having some problems getting fsolve to work. I tried using the example given in the help section but it keeps saying that my function (lambda_y1) since that is the first one it finds is unknown.

My functions are generated like this (sorry for the messy notation):

function F = myfun(lambda_y1,lambda_y2,h_1,h_2,F_c1,F_c2)

F = [g_1+607200.*lambda_1.^3-1899000.*lambda_1.^2+1976400.*lambda_1-684600;

g_2+607200.*lambda_2.^3-1899000.*lambda_2.^2+1976400.*lambda_2-684600;

lambda_y1.^2.*(4.*h_m1.^2+y_01.^2)-(4.*(h_m1-h_1).^2+(lambda_1.*y_01).^2);

lambda_y2.^2.*(4.*h_m2.^2+y_02.^2)-(4.*(h_m2+h_2).^2+(lambda_2.*y_02).^2);

F_c1.^2.*(4.*(h_m1-h_1).^2+(lambda_1.*y_01).^2)-16.*g_1^2.*(h_m1 - h_1).^2;

F_c2.^2.*(4.*(h_m2+h_2).^2+(lambda_2.*y_02).^2)-16.*g_2^2.*(h_m2 + h_2).^2];

I call it like this:
x0=[0.5,0.5,0.0005,0.0005,0.1,0.1];
f = fsolve(@myfun,x0,lambda_y1,lambda_y2,h_1,h_2,F_c1,F_c2);

All the constants except for the unknowns are given earlier.

Can anyone help me get this to run?

Lonnie
 
Physics news on Phys.org
What are the unknowns you are trying to find?
 
First of all the syntax which you are using is not correct,
most common and simple syntax is "f = fsolve(@fun,x0)", where x0 are the initial guesses.

In your case also i guess x0 are the initial guesses, the correct syntax should bex0=[0.5,0.5,0.0005,0.0005,0.1,0.1];
f = fsolve(@myfun,x0);Also, the myfun should like as follows:
it should initialize all the unknowns with their initial guesses i.e.
unknown1 = x0(1);
unknown2 = x0(2);
and so on,

also you should define all the values of the constants inside the myfun itself.
Constant1 = a;
Constant2 = b; etc.

I guess it will solve the problem.
 
Hi again,

Yep that did it, thanks a lot, but now I have run into another problem. I run my equations and get the following stopping criteria:

No solution found.

fsolve stopped because the problem appears regular as measured by the gradient,
but the vector of function values is not near zero as measured by the
default value of the function tolerance.

I get that there must be a problem with my equations, but I have no idea what to look for!

Lonnie
 
As you think, may be there is some problem with your equations, then it will be better if you check them with some known values, see if it is giving correct result.
(Also there may be the case that there is "no solution", which i think is not the case, if you are expecting some solution)
Also it will be helpful if you simplify the equations.
You will have to debug it...and see if there are any improvements.
 

Similar threads

Replies
1
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 13 ·
Replies
13
Views
7K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
6K
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
8K