MATLAB numerical evaluation doubt

In summary, you are trying to solve an equation for x using the fsolve command in Matlab, but are having issues because you need to pass in multiple values of y, L, and an initial guess for x. You could try using an anonymous function or using nested functions to make the process easier.
  • #1
rsr_life
51
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
  • #2
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.
 
  • #3
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:

Related to MATLAB numerical evaluation doubt

1. What is MATLAB numerical evaluation?

MATLAB numerical evaluation is the process of using MATLAB, a programming language and numeric computing environment, to solve mathematical problems and perform numerical calculations. It involves using built-in functions and algorithms to evaluate equations, manipulate data, and analyze results.

2. How accurate is MATLAB numerical evaluation?

The accuracy of MATLAB numerical evaluation depends on several factors, such as the precision of the input data, the complexity of the problem, and the chosen algorithm. In general, MATLAB uses double-precision floating-point numbers, which have a precision of about 15-16 decimal digits. However, it also offers symbolic math capabilities for exact results when needed.

3. Can MATLAB handle large datasets in numerical evaluation?

Yes, MATLAB is capable of handling large datasets in numerical evaluation. It has efficient data structures and built-in functions for working with arrays and matrices, making it suitable for handling and manipulating large amounts of data. It also has parallel computing capabilities for faster processing of large datasets.

4. Is MATLAB numerical evaluation suitable for all types of mathematical problems?

MATLAB numerical evaluation is well-suited for a wide range of mathematical problems, including linear algebra, calculus, statistics, and differential equations. It also has specialized toolboxes for specific applications, such as signal processing, image processing, and control systems, making it versatile for various scientific fields.

5. How can I learn more about MATLAB numerical evaluation?

There are many resources available for learning more about MATLAB numerical evaluation. You can start by reading the official documentation and tutorials provided by MathWorks, the company behind MATLAB. You can also take online courses or attend workshops and seminars to improve your skills. Additionally, there are many online forums and communities where you can ask questions and learn from other users' experiences.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
851
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
3K
Back
Top