How can I take the value from an equation and plug it back into the same equation? (Newton-Raphson)

8 ビュー (過去 30 日間)
I'm trying to implement a Newton-Raphson function into my script. The code I've created so far is;
function [ALI] = down(A, B, C, D, E, n)
A = 0.012;
B = 0.04;
C =0.06;
D = sqrt(A)/2;
n =20
for i = 1:n
E = D - F(D)/F'(D)
ALI = D-E;
end
end
The equation for E has been edited into a simpler form of the newton-raphson equation; the equation is extremely long and includes the variables A, B, & C too. I want to perform equation E in a loop, in which the value from each iteration is placed back into itself, until D minus E is within the required tolerance. However, the way I've written the function means that it just repeats the same equation over and over and I'm not sure how to change this, please could someone help?
Also, variables A, B, C and D depend on other variables, which are calculated earlier in the script.. is there a way in which I can define the variables in the function so that they take the values already calculated in the script? I'm currently having to use a caluclator for the values and then plugging them into the function. This will be a major problem later when im trying to performs loops etc. Thank you.

採用された回答

DGM
DGM 2021 年 4 月 11 日
編集済み: DGM 2021 年 4 月 11 日
Something like this:
function D = down(A, B, C, D, maxiter, tol)
% things that are in the argument list don't get rewritten
% function parameters
% A, B, C
% initial condition
% D
% process parameters
% maxiter, tol
stepsize=1;
iter=1;
while stepsize > tol
E = D - F(D)/Fprime(D); % this estimate
stepsize = abs(D-E);
D = E; % is the next point to evaluate at
iter = iter+1;
% just in case we don't converge
if iter > maxiter
break;
end
end
end
If the parameters and intial conditions are input arguments that depend on external variables, why not just precalculate them before passing them to the function?
  4 件のコメント
Bob Wake
Bob Wake 2021 年 4 月 11 日
Hi DGM, thanks again. The function I'm using calculates the inflow ratio across a helicopter main rotor. So D is an assumed value when the helicopter is hovering (sqrt(a)/2), but changes in forward flight. The equation for DA is an equation by Glauert in the form of newton-raphson. Anyway I have managed to converge the calculation by placing the first equation for D before the 'for' loop and it now works, so thank you for your help. I'm sorry for my poor explanation also.
Bob Wake
Bob Wake 2021 年 4 月 11 日
However, I'm still unsure of how to take the variables from the script, into the function..
So like I say, A, B, C are calculated using equations with other variables earlier in the script. But I want to be able to take these precalculated values into the function, without having to rewrite their corresponding equations inside the function..

サインインしてコメントする。

その他の回答 (1 件)

Jan
Jan 2021 年 4 月 11 日
Replace
E = D - F(D)/F'(D)
by
D = D - F(D)/F'(D)
For the 2nd part: You have defined the function with inputs:
function [ALI] = down(A, B, C, D, E, n)
but A,B,C,D are overwritten directly. E does not seem to be an input at all, but a locally used variable.
If you want to use the inputs, define them outside and provide them as inputs when you call your funcion:
function main
A = 0.012;
B = 0.04;
C = 0.06;
D = sqrt(A) / 2;
n = 20;
ALI = down(A, B, C, D, n)
end
function ALI = down(A, B, C, D, n)
for i = 1:n
Dold = D;
D = D - F(D)/F'(D)
ALI = D - Dold;
end
end
  3 件のコメント
Jan
Jan 2021 年 4 月 11 日
You re-define D with the initial value in each iteration:
for i = 1:n
D = sqrt(A)/2;
...
Move the line "D = sqrt(A)/2;" before the loop - exactly as in the FORTRAN code.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by