Iterative solution to approach desired value

Hello,
I would like to find desired value Q = 7.76 using starting external solver in my Matlab script. Input for the external solver is value p. From several runs of the external solver I found the following values of p = pQ(:,1) and corresponding Q = pQ(:,2). How to find desired value 7.76? I found several examples of algorithms like Newton-Raphson but they use analytical equation, what I don't have here. Could you help to write such script?
Best regards
Michal
pQ = [
0.99471e+5 20.014;
0.99871e+5 4.89;
99795.887596 7.735;
99795.941823 7.733;
99795.964903 7.732;
99795.976825 7.731;
99796.01 7.73
]

 Accepted Answer

Walter Roberson
Walter Roberson on 19 May 2022
Edited: Walter Roberson on 19 May 2022
fzero() or fsolve().
If you need to be able to look at the complete source code then https://www.mathworks.com/matlabcentral/fileexchange/72478-bisection-method
Note that when you do not reply to my suggestions such as https://www.mathworks.com/matlabcentral/answers/1721605-iterative-solution-to-achieve-convergence#comment_2165925 then it is difficult for the volunteers to guess what you need different than what has already been suggested.

8 Comments

Hello Walter,
thank you very much for your help. A reason why I did not reply in previous topic is that I think that my original question was different than what I really need. And I urgently search for solution of this problem because it already lasts for weeks. Now back to the topic: in all example of some search methods like Newton Raphson, bisection and so on there is at the beginning some function like x^2 and they try to find its root. However, I have no such function. I can run several times external solver with various values p from Matlab and this way I can find corresponding Q. These values are shown above. And I want to find such p that Q is as close to 7.76 as possible with given accuracy. So can I use fzero or fsolve if they require some analytical function and I don't have it?
If - given a value for p - you can somehow calculate the corresponding Q, you implicitly have a function
Q = f(p)
Maybe the form of f is too complicated to write it in one short formula, but if you call fzero as
p0 = ... ; % some start value for p such that f(p) = 7.76
p = fzero(@fun,p0)
and use the function "fun" like
function res = fun(p)
% Calculate Q as a function of p
% ...
res = Q - 7.76;
end
you will get your result.
Hello Torsten,
thank you very much! I have tested your proposal and it runs solution again and again but with the same value of p0 from the beginning.
I will show here how my script look like. Probably I am doing somethin wrong but I don't know what.
Firstly I will show the function:
function res = fun(p)
% Calculate Q as a function of p
run external_solver
results = readtable('Results.txt')
Q = results.Q
p = results.P
res = Q - 7.76;
end
Here is my script:
p0 = 99.925e+3 ; % some start value for p such that f(p) = 7.76
save('p_value.mat','p0')
p = fzero(@fun,p0)
A problem is probably because the external solver uses always p0 from beginning which was saved befor pzero command and it is not informed about changed value of p. How to get current value of p before starting new iteration with external solver? In the second line I am saving p0 value so the external solver could load it and use. Probably this is missing here and therefore it does not work.
And
p0 = 99.925e+3 ; % some start value for p such that f(p) = 7.76
p = fzero(@fun,p0)
function res = fun(p)
% Calculate Q as a function of p
save('p_value.mat',num2str(p))
run external_solver
results = readtable('Results.txt')
Q = results.Q
res = Q - 7.76;
end
does not work ?
How you pass this parameter to the external solver will depend on what mechanism the external solver offers to interface to it.
From the looks of it, it seems like your external solver expects the value of p to come from a MAT-file named p_value.mat. If that's the case your objective function would need to:
  1. accept the p value passed to it by fzero or fsolve
  2. save that value to the MAT-file
  3. call the external solver
  4. read in the output from the solver (the Results.txt file), and
  5. return the output to fzero or fsolve.
If your external solver had a COM interface or could accept the input parameter via a command-line switch or something you might be able to avoid creating and recreating the MAT-file.
Hello,
second proposal works perfectly! Thank you very much for your help.
There is only one little issue which I found out recently. The value p can not be higher than 1e5 because external solver can not converge. The best would be to search in interval <0.9e5,1e5>. Is possible to specify this for fzero() ?
I restricted the search interval to (90000:100000):
p0 = 99.925e+3 ; % some start value for p in [90000:100000] such that f(p) = 7.76
p0_trans = tan( (p0-95000)/5000 * pi/2);
p = fzero(@fun,p0_trans);
p_backtrans = 2/pi*atan(p)*5000 + 95000
function res = fun(p)
p_backtrans = 2/pi*atan(p)*5000 + 95000;
% Calculate Q as a function of p
save('p_value.mat',num2str(p_backtrans))
run external_solver
results = readtable('Results.txt')
Q = results.Q
res = Q - 7.76;
end
Thanks a lot for your help!

Sign in to comment.

More Answers (0)

Products

Release

R2020b

Community Treasure Hunt

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

Start Hunting!