MATLAB MATLAB numerical evaluation doubt

AI Thread Summary
The discussion revolves around using MATLAB's fsolve function for numerical evaluation of the equation L = K(x) - F(x,y), with the goal of finding x for varying values of y and L. The user initially struggled to pass multiple values of y and L to fsolve, but found success using an anonymous function. However, they encountered warnings during iterations, suggesting potential issues with the symbolic variables and integration in their functions. The user attempted to resolve these warnings by using the double(int(...)) command, which improved the situation but did not eliminate the errors entirely. Suggestions for further troubleshooting include refining the initial guess and ensuring that the functions are correctly defined to avoid complications with symbolic computations.
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
5
Views
3K
Replies
7
Views
2K
Replies
2
Views
3K
Replies
4
Views
2K
Replies
13
Views
2K
Replies
1
Views
5K
Back
Top