- The new guess x0 is displayed with the required precision using the fprintf statement.
- A counter variable is iterated to find the number of loop executions.
Add code to a function that finds roots of an equation using the Newton-Raphson method
5 ビュー (過去 30 日間)
古いコメントを表示
Add code to a function that finds roots of an equation using the Newton-Raphson method
- Modify the code to display the new "guess" value on each iteration of the loop (i.e., display the value of X). Display the value with at least 15 digits of accuracy after the decimal point. (This will require the use of the fprintf() function.)
- Add a counting variable that counts the number of times the loop executes and display this value after the loop terminates.
%this function will find roots of an equation using the Newton-Raphson method
Tolerance = 10e-15;
X = input('Please enter your best guess where a root might be: ');
while abs(f(X)) > Tolerance
X = X - f(X)/f_derivative(X);
end
fprintf('Root: f(%0.15f) = %0.15f\n', X, f(X));
Count = 1
while Count <= 10
disp(Count)
Count = Count + 1
end
I have this newton-raphson code, but I need some guidance because I know there's more to it.
0 件のコメント
回答 (1 件)
Ajay Pattassery
2020 年 5 月 7 日
Hello,
Consider the following simple example I wrote.
syms f x; %Defined as symbolic variables
f = x.^2 -4*x + 4;
tolerance = .005;
x0 = 16;
count = 0;
while(subs(f,x0) > tolerance) %subs is used here for substituting the value x0 to the function f, differential(f)
x0 = x0 - (subs(f,x0)/subs(diff(f),x0));
fprintf(1,'x0 = %.15f\n',x0)
count = count + 1;
end
fprintf('Number of times loop executed = %d\n',count)
Here I am finding the roots of the simple equation (x-2).^2 = 0
I have incorporated your queries in the above code by taking a sample function f.
Hope this helps!
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!