MATLAB numerical evaluation doubt

Click For Summary
SUMMARY

The discussion focuses on using MATLAB's fsolve function for numerical evaluation of the equation L = K(x) - F(x,y). The user seeks to determine the value of x for multiple values of y and L. The solution involves creating an anonymous function to encapsulate the variables and using a loop to iterate through values of L. The user encountered warnings related to the fsolve function, particularly when integrating with symbolic variables, and sought advice on improving the robustness of their implementation.

PREREQUISITES
  • Familiarity with MATLAB programming
  • Understanding of numerical methods, specifically fsolve
  • Knowledge of anonymous functions in MATLAB
  • Basic concepts of symbolic computation in MATLAB
NEXT STEPS
  • Explore MATLAB's documentation on fsolve for advanced options and settings
  • Learn about creating and using anonymous functions in MATLAB
  • Investigate techniques for estimating initial guesses for numerical solvers
  • Review best practices for handling symbolic variables in numerical computations
USEFUL FOR

MATLAB users, numerical analysts, and engineers who need to solve nonlinear equations and optimize numerical methods in their projects.

rsr_life
Messages
48
Reaction score
0
Hi! I need help from someone that knows something about numerical methods in MATLAB. Specifically, fsolve.

So, I have an equation L = K(x) - F(x,y). Given the values of L and y, I need to determine the value of x numerically.

So, here's what I've done:

G(x) = L- K(x) + F(x,y)
in the function m-file
function [G] = g(x,y,L)

I then need to evaluate G(x) numerically to find the values of x at which G(x) becomes 0. Thats my understanding of numerical determination.

So I use the fsolve command in Matlab this way
[x, fval] = fsolve(@g, x, options);​

But the problem is that I need to pass multiple values of y, L and some initial estimate of x (that apparently fsolve requires) to find different values of x for different values of y. Matlab doesn't accept this. i.e. ideally I'd like to do something like

for y=0:0.1:10

[x, fval] = fsolve(@g(x,y,L), x, options);​
end​

where I can pass multiple values of y and L (array or for-loop) to find the values of x.

Can I do this? or do I need to declare Global variables and access y and L in function g that way?

Also, if I've no idea of what x looks like, how do I estimate an initial value?

Would appreciate your contributions to resolving this.

Thanks.
 
Physics news on Phys.org
There are various aways to do this.
The simplest way is to use an anonymous function (look in the help-files, as far as I remember fsolve is used to illustrate how they are used). Or, alternatively, use nested functions (where you have more than one function per file, and access to all the variables). Personally I prefer the former.

About finding a first guess: Try plotting the function for different values for L and y; the inital guess only needs to be in the "neighborhood" in order for fsolve to be efficient.
 
Hey thanks,

I created and used the anonymous function and it worked out fine.

for L=0:15
g = @(x) L-K(x)+F(x,y)
[x] = fsolve(g, x, options)
end​

It worked fine except when Matlab threw some warnings at some of the iterations:
Warning, unable to determine if -4503599627370496*pi*(1+2*_Z13)/(-4503599627370496*pi+1746276232035673*i) is between 0 and 1; try to use assumptions or set option _EnvAllSolutions to true


Setting that option to true didn't help.

Is this an error with the way I've used fsolve? I'm using the int() command for integration in one of the functions and using syms too. The symbolic variables can sometimes cause fsolve to throw errors like these, but I've tried resolving it using double(int(...)). It worked better than the first time (meaning more iterations went through), but sort of just collapsed later.

Any suggestions?
 
Last edited:

Similar threads

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