newton's method to solve nonlinear equation

1 Ansicht (letzte 30 Tage)
N/A
N/A am 10 Dez. 2020
Kommentiert: N/A am 10 Dez. 2020
hw prompt is below my work. please check my code. i am new to using newton's method and need some recommendations about what to do or fix. thank you.
what i did first:
x=[-3:0.01:3];
f = sin(x)-x.*cos(x);
plot(x,f);
what i did next:
x=0;
Tol = 0.0000001;
count = 0;
dx=0;
f=sin(0)-0*cos(0);
fprintf('step x dx f(x)\n')
fprintf('---- ----------- --------- ----------\n')
fprintf('%3i %12.8f %12.8f %12.8f\n',count,x,dx,f)
while (dx > Tol)
count = count + 1;
fprime = x.*sin(x);
xnew = x - (f./fprime);
dx=abs(x-xnew);
x = xnew;
f = sin(x)-x.*cos(x);
fprintf('%3i %12.8f %12.8f %12.8f\n',count,x,dx,f)
end
on the other homework problems, i received several answers, yet on this problem i received only one. did i make a mistake?

Akzeptierte Antwort

John D'Errico
John D'Errico am 10 Dez. 2020
Bearbeitet: John D'Errico am 10 Dez. 2020
Is there a good reason why you initialized dx to be zero?
Do you see that immediately after that, you start a while loop. What is the codition?
while (dx > Tol)
Now, ask yourself. What was dx again when the loop started?
What is the value of Tol?
Is this true? (dx > Tol)?
Do you expect the while loop to ever allow entrance into the code inside that loop? What would happen if you wrote this code fragment?
while false
disp('The sky is falling! (sincerely, Chicken Little)')
end
How many times would you expect to see anything written to the command window?
  4 Kommentare
N/A
N/A am 10 Dez. 2020
1 0.33048772 0.16951228 0.01190128
2 0.21951457 0.11097316 0.00350893
3 0.14610690 0.07340767 0.00103744
4 0.09733515 0.04877175 0.00030710
5 0.06486959 0.03246556 0.00009095
6 0.04324032 0.02162926 0.00002694
7 0.02882509 0.01441524 0.00000798
8 0.01921619 0.00960889 0.00000237
9 0.01281064 0.00640555 0.00000070
10 0.00854038 0.00427026 0.00000021
11 0.00569357 0.00284681 0.00000006
12 0.00379571 0.00189786 0.00000002
13 0.00253047 0.00126524 0.00000001
14 0.00168698 0.00084349 0.00000000
15 0.00112465 0.00056233 0.00000000
16 0.00074977 0.00037488 0.00000000
17 0.00049985 0.00024992 0.00000000
18 0.00033323 0.00016662 0.00000000
19 0.00022215 0.00011108 0.00000000
20 0.00014810 0.00007405 0.00000000
21 0.00009874 0.00004937 0.00000000
22 0.00006582 0.00003291 0.00000000
23 0.00004388 0.00002194 0.00000000
24 0.00002925 0.00001463 0.00000000
25 0.00001950 0.00000975 0.00000000
26 0.00001300 0.00000650 0.00000000
27 0.00000867 0.00000433 0.00000000
28 0.00000578 0.00000289 0.00000000
29 0.00000385 0.00000193 0.00000000
30 0.00000257 0.00000128 0.00000000
31 0.00000171 0.00000086 0.00000000
32 0.00000114 0.00000057 0.00000000
33 0.00000076 0.00000038 0.00000000
34 0.00000051 0.00000025 0.00000000
35 0.00000034 0.00000017 0.00000000
36 0.00000023 0.00000011 0.00000000
37 0.00000015 0.00000008 0.00000000
N/A
N/A am 10 Dez. 2020
This answer seems more right. Thank you for your recommendations. Please let me know if you have another.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Scope Variables and Generate Names finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by