필터 지우기
필터 지우기

print to output file

조회 수: 6 (최근 30일)
Ayda
Ayda 2011년 11월 22일
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개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by