I have a problem with fprintf function, MATLAB
Ältere Kommentare anzeigen
Hello,
I have a question about my matlab code. I wrote my code, but when I run my code, output look is wrong.
I want a output that gives me my results in tabulated form in which the first column shows the number of terms ( in my code m represents the term) , the second column lists the approximation value ( in my code sum represent the app. value), third column shows the true value( in my code truevalue represents that), fourth column show the Et true percent relative error (in my code Et represent that) and finally fifth column represents the Ea error (in my code Ea represents that).
But when I run my code, the output is very wrong.
Thanks for the help.
clc
clear all
close all
x = -2.5;
truevalue = log(exp(1)+x); %my true value is -1.5220
Ea= 100;
Es = 0; %First error
m=1;
sum=1;%sum is represents the formula of ln(e+x) given in the question.
fprintf('\tn\t app.\t true value\t Et\t Ea\n')
while abs(Ea) >= Es
Es = (0.5*10^-2)/100;
sump = sum;
sum = sum + (((-1)^(m-1))/m)*((x/m)^m); %sum is my approximate value
Et = abs(( ( truevalue - sum ) / truevalue )*100);
Ea = (( sum - sump ) / sum ) * 100;
fprintf('%10.4f\t\t %10.4f\t %10.4f\t\t %10.4f\n',m,sum,truevalue,Et,abs(Ea))
m = m+1;
end
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Programming finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!