You might be curious to know how many times fzero calls your function, and where. The second argument is a vector that contains two elements. > -2)Īlternatively, if you know two values that bracket the root, you can provide both. If we provide a different value, we get a different root (at least sometimes). The second argument is the initial value. In turn, fzero calls your error function-more than once, in fact. The interesting thing here is that you’re not actually calling error_func directly you’re just telling fzero where it is. The symbol allows us to name the function without calling it. The first argument is a function handle that specifies the error function. You can call error_func from the Command Window and confirm that \(3\) and \(-1\) are zeros: > error_func(3)īut let’s pretend that we don’t know where the roots are we only know that one of them is near 4. To use fzero you have to define a MATLAB function that computes the error function, like this: function res = error_func(x) Values of \(x\) where \(f(x) = 0\) are called zeros of the function or roots. This function is useful because we can use values of \(f(x)\), evaluated at various values of \(x\), to infer the location of the solutions. The value of the error function is 0 if \(x\) is a solution and nonzero if it is not. In order to use it, we have to rewrite the equation as an error function, like this: The MATLAB function fzero that uses numerical methods to search for solutions to nonlinear equations. We’ll see a better alternative in the next section. But it doesn’t always work, and it’s not often used in practice. The nice thing about the method we just used is that it’s simple. Techniques that generate numerical solutions are called numerical method. )Īfter each iteration, x is closer to the correct answer, and after five iterations the relative error is about 0.1 percent, which is good enough for most purposes. \) to compute successive approximations of the solution.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |