How to find the nth root of equation?
Mostra commenti meno recenti
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);
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Develop Apps Using App Designer in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!