MATLAB The following question needs to be programmed in MATL
(MATLAB) The following question needs to be programmed in MATLAB.
Please make sure the output is giving out all of its iterations! Thank you.
1. Using the procedure Newton and a single computer run, test your code on these examples: f(t) = tan-t with xo = 7 and g (t) = et-Vt + 9 with x0 = 2. Print each iterate and its accompanying function value.Solution
Matlab code for newton method with output after execution
a=input(\'Enter the function in the form of variable x:\',\'s\');
x(1)=input(\'Enter Initial Guess:\');
error=input(\'Enter allowed Error:\');
f=inline(a)
dif=diff(sym(a));
d=inline(dif);
fprintf(\'step x error\ \')
fprintf(\'---- ----------- --------- ----------\ \')
for i=1:100
x(i+1)=x(i)-((f(x(i))/d(x(i))));
err(i)=abs((x(i+1)-x(i))/x(i));
fprintf(\'%3i %12.8f %12.8f \ \',i,x(i+1),err(i))
if err(i)<error
break
end
end
root=x(i)
Output for 1st input
Enter the function in the form of variable x:tan(x)-x
Enter Initial Guess:7
Enter allowed Error:0.001
f =
Inline function:
f(x) = tan(x)-x
step x error
---- ----------- --------- ----------
1 15.07002609 1.15286087
2 43.83949106 1.90905210
3 2171.29891018 48.52837859
4 11086.90102199 4.10611458
5 231659.76783343 19.89490719
6 253259.13015334 0.09323743
7 2023412.35869542 6.98949423
8 8963098.40581300 3.42969441
9 35881724.24120038 3.00327237
10 37156204.38825189 0.03551892
11 117773622.71673778 2.16968928
12 121713444.83502489 0.03345250
13 283297835.96657705 1.32758046
14 4891328117.55537800 16.26567413
15 5568344188.59267810 0.13841150
16 61987176883.68338000 10.13206634
17 94602999408.68574500 0.52617048
18 100211513916.84033000 0.05928474
19 367818982450.42151000 2.67042636
20 37996368869699.76600000 102.30181606
21 38621258204276.01600000 0.01644603
22 337926452153131.19000000 7.74975254
23 398616508862452.50000000 0.17959546
24 846085991496279.50000000 1.12255632
25 918130371181413.12000000 0.08515019
26 10702894242044980.00000000 10.65727066
27 91963995868264432.00000000 7.59244180
28 1718846885683703800.00000000 17.69043281
29 1811599828589238300.00000000 0.05396231
30 3833168333892160500.00000000 1.11590235
31 76696631576147214000.00000000 19.00867817
32 81064534011530707000.00000000 0.05695038
33 136170292921732170000.00000000 0.67977642
34 306988918455761900000.00000000 1.25444854
35 930192871258792790000.00000000 2.03005358
36 487231127060844850000000.00000000 522.79580850
37 609019583453020550000000.00000000 0.24996034
38 634400886584175620000000.00000000 0.04167568
39 357774932478857040000000000.00000000 562.95717605
40 360136528630456040000000000.00000000 0.00660079
41 3425381767664841700000000000.00000000 8.51134221
42 3473908313191987700000000000.00000000 0.01416676
43 29071576188277501000000000000.00000000 7.36855022
44 47855238582853156000000000000.00000000 0.64611778
45 158467941280189770000000000000.00000000 2.31140218
46 4413345557569528600000000000000.00000000 26.85008451
47 7870568869786026800000000000000.00000000 0.78335659
48 12094504451906565000000000000000.00000000 0.53667475
49 15733268821445275000000000000000.00000000 0.30086097
50 34431935943244810000000000000000.00000000 1.18847948
51 302313254051685610000000000000000.00000000 7.78002487
52 2472658625045204400000000000000000.00000000 7.17912742
53 2974696358377544000000000000000000.00000000 0.20303560
54 5090435375277735600000000000000000.00000000 0.71124537
55 5095302288487629200000000000000000.00000000 0.00095609
root =
5.0904e+33
Output for 2nd Input
Enter the function in the form of variable x:exp(x)-sqrt(x+9)
Enter Initial Guess:2
Enter allowed Error:0.001
f =
Inline function:
f(x) = exp(x)-sqrt(x+9)
step x error
---- ----------- --------- ----------
1 1.43737741 0.28131129
2 1.19595246 0.16796212
3 1.15989172 0.03015232
4 1.15918958 0.00060534
root =
1.1599



