How to find the nth root of equation?

3 ビュー (過去 30 日間)
Andrey Kryukov
Andrey Kryukov 2021 年 12 月 5 日
コメント済み: Andrey Kryukov 2021 年 12 月 12 日
HI
I have already written a program which can easily find the x coordinates of the first intersection point of two given functions. But how to find nth intersection point? (I want to input n value from keyboard). Here is my program: (what I need to change?)
clear;clc;eps = 0.001; x = 0; dx = 0.1; dY = 1; i = 0; j = 1;
prompt = 'Input a number of the needed root, please.';
n = input(prompt);
while dY > 0
i = i + 1;
xt(i) = x;
Y1 = cos(x);
Y2 = sqrt(x)/100;
yt1(i) = Y1;
yt2(i) = Y2;
dY = Y1 - Y2;
x = x + dx;
end
y1r(1) = yt1(i);
y1r(2) = yt1(i - 1);
y2r(1) = yt2(i);
y2r(2) = yt2(i - 1);
xr(1) = xt(i - 1);
xr(2) = xt(i);
for n = 1:2
xrt(n) = xr(n);
end
while abs(dY) > eps
xc = (xr(1) + xr(2))/2;
Y1 = cos(xc);
Y2 = sqrt(xc)/100;
dY = Y1 - Y2;
if dY > 0
xr(1) = xc;
else
xr(2) = xc;
end
j = j + 1;
end
disp('Y1 = cos(x)');
disp('Y2 = sqrt(x)/100');
disp('xc = ');disp(xc);
disp('Y1 = ');disp(Y1);
disp('Y2 = ');disp(Y2);
disp('Number of interations: ');disp(j);
plot(xrt,y1r,xrt,y2r);

採用された回答

Alagu Sankar Esakkiappan
Alagu Sankar Esakkiappan 2021 年 12 月 8 日
編集済み: Alagu Sankar Esakkiappan 2021 年 12 月 8 日
Hi Andrey,
I see that you're trying to identify two end points [ xr(1) xr(2) ] of an intersection first and then narrowing down the end points to find the exact intersection point xc. It is fairly easy to find out nth intersection point for two curves ( given it exists ). A slight algorithm change is required in both end point calculation and intersection point calculation.
An intersection point xc exists between x1 and x2 when dY changes its sign for inputs x1 and x2. i.e, Either a +ve to -ve transition or vice versa. In the current algorithm, only the +ve to -ve transition is considered. It may be changed based on the following reference code:
currentSign = ( dY > 0 ); % Initialization for current dY signature
previousSign = currentSign; % Initialization for previous dY signature
for k = 1:n % Repeat End point identification 'n' times
while (previousSign == currentSign) % Check if dY has same signature compared to previous iteration
previousSign = currentSign;
i = i + 1;
xt(i) = x;
Y1 = cos(x);
Y2 = sqrt(x)/100;
yt1(i) = Y1;
yt2(i) = Y2;
dY = Y1 - Y2;
currentSign = ( dY > 0 );
x = x + dx;
end
% Loop terminates when there is a +ve to -ve transition or vice versa
previousSign = currentSign;
end
Now that End points [ xr(1) xr(2) ] for nth intersection point is identified, the exact intersection point can be found out. Before proceeding to next step, Re-arrange end points according to [ xr_for_+ve_dY xr_for_-ve_dY ]. Doing so, we will ensure that we have two end points that has an intersection point between them.
% Find End Points xr(1) and xr(2) before this line
Y1 = cos(xr(1));
Y2 = sqrt(xr(1))/100;
if (Y1 - Y2) > 0
xr = [xr(1) xr(2)]; % Keep x input for +ve dY in 1st index and -ve dY in 2nd index
else
xr = [xr(2) xr(1)]; % Swap values if this is not the case
end
% Calculate Intersection point xc after this line
You may then proceed to calculate xc as before to find out nth root for your equation.
  1 件のコメント
Andrey Kryukov
Andrey Kryukov 2021 年 12 月 12 日
Thank you! Great explanation!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by