fzero implicit functions - how to make recursive function handles

3 次查看(过去 30 天)
I've been trying to implement a discrete ODE solver, but my iterations require an implicit function (f2), which also depends on the output of another function(f1)'s output with it's value as that function's input (i.e., f2(x) = ...f2(x). The function f1 will be an arbitrary handle which is the ODE.
Now, I do know that f2 will not be able to assign itself a handle, because f2 doesn't exist in the first place to refer to itself. But I do need this kind of a recursion because my implicit function demands it.
A simple version of the relevant part of the code is as follows, I've made f1 a sine, typically, it will be an input.
function x2 = fn(x)
f1 = @(x) sin(x);
f2 = @(x) x+f1(x)+f1(f2(x));
x2 = fzero(f2,x);
end
What would be the proper way to write line 3?
Thanks in advance.

采纳的回答

Walter Roberson
Walter Roberson 2021-6-16
You cannot. You are missing an essential part of recursion: there must always be a termination condition. Anonymous functions cannot themselves code tests that prevent recursive calls at the termination condition: you need a non-anonymous function.
  3 个评论
Walter Roberson
Walter Roberson 2021-6-16
I am not sure that you do understand.
You do not proceed with your approach. You do not try to work around it by finding some way to make the handle visible to the function (which you can do if you use real functions), because your calculation would never end.
You want
f2(x) = x + f1(x) + f1(f2(x))
for a given x, this is
X = x
F1X = f1(x)
F2 == X + F1X + f1(F2)
X + F1X + f1(F2) - F2 == 0
so
obj = @(F2) X + F1X + f1(F2) - F2
F2 = fzero(obj, x0)
After that you have
x2 = fzero(f2,x);
so you want to solve for the x that makes F2 equal to 0. But if you know that F2 is equal to 0, then substitute that in:
f2(x) == x + f1(x) + f1(f2(x))
0 == x + f1(x) + f1(0)
magicx = fzero(@(x) x + f1(x) + f1(0), x0)
and this is independent of the x in
function x2 = fn(x)
unless different x for fn(x) correspond to different f1 functions, or unless different x for fn(x) correspond to different starting points for the fzero() search
... and f2 never needs to be calculated.
Atakan Botasun
Atakan Botasun 2021-6-16
So, looking at your response, I've figured out that I've done a small mistake in defining f2, as in f2 == 0 is a condition, so f2 itself must be subtracted from my expression. The solution was not in messing with handles thanks to your explanation, but parametrizing one that has two variables, as I already know one of the points. For the other point, I've made fzero start guessing from the previous point after parametrization.
Here's the new version (that has considerably more information about the problem itself), I'm still working on it to understand if it is adequate, but there are no major errors:
function [k1, k2, x2] = fn2(x1)
f = @(x) -x^2*cos(x)*sin(x);
f2 = @(y,x) x+.5*(f(x)+f(y))-y;
fun = @(y)f2(y,x1);
x2 = fzero(fun,x1);
k1 = f(x1);
k2 = f(x2);
end
So this time I didn't forget to subtract the next iterate (renamed as y this time) from itself to get the handle f2 to represent something that is supposed to be zero. Parametrizing with the known value for x which is x1, I've obtained x2 using fzero, the only caveat I'm worried about being the initial guess. I've compared my results with the Runge-Kutta 4 solver to feel safer, doesn't look half bad.
So, thanks for making the inner workings of fzero a bit clearer to me.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Configure Simulation Conditions 的更多信息

产品


版本

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by