Azzera filtri
Azzera filtri

Adding Tolerence to Bisection Code

2 visualizzazioni (ultimi 30 giorni)
Bailey Smith
Bailey Smith il 13 Giu 2018
Commentato: Bailey Smith il 13 Giu 2018
I am writing a code that needs a tolerance of 0.000005. My function is (currently using equation 3):
function [out] = f(x)
%returns roots
%out=exp(x)-(x^2+4); %Equation 1
%out=x - 2^(-x); %Equation 2
out=x^3 - 1.8999*x^2 + 1.5796*x - 2.1195; %Equation 3
end
I am working of adding another column, f(P), to my code. Here it is:
clear; clc;
nr=input('Enter Number of Iterations: ');
A=input('Enter Value of A: ');
B=input('Enter Value of B: ');
n=1;
fprintf('\n');
i=(1:nr);
%loops
if f(A)*f(B)>0
fprintf('Error. Values Cannot Be Iterated.');
else
%print column header
fprintf('\n \t');
fprintf(' A\t\t\t\t B \t\t\t P \t\t\t f(P)');
fprintf('\n\n');
while n<=nr
fprintf('%.0f\t',i(n));
P=(A+B)/2;
%Add F=f(P) formulation here? Maybe?
toprow=[A(n),B(n),P(n)]; %Add F(n)
fprintf('% 3.6f\t\t\t',toprow);
if f(A(n))*f(P(n))<0
A(n+1)=A(n);
B(n+1)=P(n);
elseif f(P(n))*f(B(n))<0
A(n+1)=P(n);
B(n+1)=B(n);
end
fprintf('\n');
n=n+1;
end
end
This is the output that I desire:
The root to the equation f= x^3-1.8999*x^2+1.5796*x-2.1195 =0 on [1,2] was found to be 1.703133 where f(1.703133)=0.000013.
The 17 iterations for the bisection routine were recorded as:
n a b p f(p)
1 1.000000 2.000000 1.500000 -0.649875
2 1.500000 2.000000 1.750000 0.185731
3 1.500000 1.750000 1.625000 -0.278558
4 1.625000 1.750000 1.687500 -0.058767
5 1.687500 1.750000 1.718750 0.060302
6 1.687500 1.718750 1.703125 -0.000016
7 1.703125 1.718750 1.710938 0.029946
8 1.703125 1.710938 1.707031 0.014916
9 1.703125 1.707031 1.705078 0.007437
10 1.703125 1.705078 1.704102 0.003708
11 1.703125 1.704102 1.703613 0.001845
12 1.703125 1.703613 1.703369 0.000914
13 1.703125 1.703369 1.703247 0.000449
14 1.703125 1.703247 1.703186 0.000216
15 1.703125 1.703186 1.703156 0.000100
16 1.703125 1.703156 1.703140 0.000042
17 1.703125 1.703140 1.703133 0.000013
Sorry to take up so much space! I just want to be sure that I add enough information in so that I can get a little help. So, basically, I would like to add in a tolerance of 0.000005 to the code, and I would like for the code to stop and print the following type output when it hits the root. I feel like that would include an 'if P==0' statement added also, but I'm not quite sure. Any and all help and pointers is appreciated! Thank you!
  2 Commenti
Basil Saeed
Basil Saeed il 13 Giu 2018
You're saying "when it hits the root". Do you mean that you want your function to stop if it is within 0.000005 of the root even if it reaches that before n iterations? (Keep in mind that you might also not find the correct solution after n iterations. How should you handle cases like that?)
Bailey Smith
Bailey Smith il 13 Giu 2018
Yes. And the numbers that will be used do have roots before the 100th iteration, but I suppose I should add an 'else' statement saying that the root has not been found (just in case). Thank you for that advice. I have been working on it and I think I almost have it completely figured out, though. I'm just stuck on printing the equation from the function on the 'The root to the equation...' part.

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su General Applications in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by