print to output file
3 views (last 30 days)
Show older comments
Good evening\morning
I wrote a code that will give me a root for a function using Bisection Method and False Position Method
I want to print on output file the follownig
k \t x2 \t absolute_c \t mid\t absolute_m\t
i tried to print it using
result= [i;x2;absolute_m;mid;absolute_m]; fprintf(fid,'%.2f\t\t\t %.6f\t\t %.6f\t\t\t %.6f\t\t %.6f\t\t\n',i,result); but it didn't work
how can I print this?
fid = fopen('hw6.txt','w');
x0 = input('inter x0 = ');
x1 = input('inter x1 = ');
tolerance=input('inter the tolerance, tolerance = ');
f = @(x) sin(2*pi*x)+ exp(1.2*x) + x - 2.5;
copy_x0= x0;
copy_x1= x1;
% Using Bisection
for i=0:inf
x2= (x0+x1)/2
c= f(x2)
absolute_c= abs(c)
if absolute_c < tolerance
break
end
if f(x0)*c <0
x1=x2;
continue
else
x0=x2;
continue
end
end
% Using False Position
for i=0:inf
mid= copy_x1 - (f(copy_x1)* (copy_x1-copy_x0)/(f(copy_x1)-f(copy_x0)))
m = f(mid)
absolute_m= abs(c)
if absolute_m < tolerance
break
end
if f(copy_x0)*m <0
copy_x1=mid;
continue
else
copy_x0=mid;
continue
end
end
fclose(fid)
0 Comments
Answers (0)
See Also
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!