Iterative solution to approach desired value

1 view (last 30 days)
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
Torsten
Torsten on 19 May 2022
Edited: Torsten on 19 May 2022
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

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!