Write Tabular Data to Text File Write a short table of the exponential function to a text file called exp.txt.

A = input('Enter A: ');
B = input('Enter B: ');
func = @(x) exp(x)-2*cos(x);
e = 1;
while e>0.0001
display(A)
display(B)
C = (A+B)/2
FC = func(C);
FB = func(B);
if(FC*FB>0)
B = C;
else
A = C;
end
e = (abs((FB-FC)/FB))*100
fprintf(fileID,'%6s %12s\r\n','A','B','C','e');
end

4 Comments

I would like the program to display square like this
i m f(m) error
1 1.500000 3.875000 0.500000
2 1.750000 6.609375 0.250000
3 1.875000 8.216797 0.125000
4 1.937500 9.085693 0.062500
5 1.968750 9.537079 0.031250
6 1.984375 9.767086 0.015625
7 1.992188 9.883178 0.007812
8 1.996094 9.941498 0.003906
9 1.998047 9.970726 0.001953
10 1.999023 9.985357 0.000977
11 1.999512 9.992677 0.000488
12 1.999756 9.996338 0.000244
13 1.999878 9.998169 0.000122
14 1.999939 9.999084 0.000061
15 1.999969 9.999542 0.000031
16 1.999985 9.999771 0.000015
17 1.999992 9.999886 0.000008
18 1.999996 9.999943 0.000004
19 1.999998 9.999971 0.000002
20 1.999999 9.999986 0.000001

Sign in to comment.

Answers (1)

Edit
A = input('Enter A: ');
B = input('Enter B: ');
func = @(x) exp(x)-2*cos(x);
e = 1;
fileID=fopen('fic.txt','w')
fprintf(fileID,'%s \t %s\t %s\t %s \r\n','i','m','f(m)','error');
while e>0.0001
display(A)
display(B)
C = (A+B)/2
FC = func(C);
FB = func(B);
if(FC*FB>0)
B = C;
else
A = C;
end
e = (abs((FB-FC)/FB))*100
fprintf(fileID,'%6f %6f %6f %6f\r\n',[A,B,C,e]);
end
fclose(fileID)

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Asked:

on 6 Jul 2015

Commented:

on 7 Jul 2015

Community Treasure Hunt

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

Start Hunting!