How do i get the table to access all my iterative data?
3 views (last 30 days)
Show older comments
This is for an assignment and i was wondering hw i could get the code to display all the interations of the x value and associated outputs in the table rather than just the final result. Thank you.
%xi = iterations
%ap = approximate value
%pe = percet error recorded
% pe desired 1*10^-5
x= -0.95;
while(1)
x= x+0.05;
if x>=0.95, break, end
end
true = log((1+x)/(1-x));
% true = calc value of the function used.
sumx=0;
xi = 0;
n = 0;
while(2)
xi = xi+1;
n = n+1;
sumx = sumx+(2*((x.^(2*n-1))/(2*n-1)));
% runs infinte series.
ap = sumx;
pe = abs((true-ap)/true)*100;
% above is error determination formula.
if pe<=0.00001 | xi>= 200, break, end
% finalizes loop set up
end
TT = [x;xi;ap;pe]';
fprintf('\n');
fprintf('x Terms used approx Percent error\n');
fprintf('%2f%7d%13.4f%12.6f',TT);
plot(true,x)
title('plot of f(x) to x')
xlabel('values of y')
ylabel('values of x')
grid
0 Comments
Answers (1)
Walter Roberson
on 12 Jul 2020
%xi = iterations
%ap = approximate value
%pe = percet error recorded
% pe desired 1*10^-5
x= -0.95;
while(1)
x= x+0.05;
if x>=0.95, break, end
end
true = log((1+x)/(1-x));
% true = calc value of the function used.
sumx=0;
xi = 0;
n = 0;
TT = zeros(4,0);
while(2)
xi = xi+1;
n = n+1;
sumx = sumx+(2*((x.^(2*n-1))/(2*n-1)));
% runs infinte series.
ap = sumx;
pe = abs((true-ap)/true)*100;
TT(:,end+1) = [x;xi;ap;pe];
% above is error determination formula.
if pe<=0.00001 | xi>= 200, break, end
% finalizes loop set up
end
fprintf('\n');
fprintf('x Terms used approx Percent error\n');
fprintf('%2f%7d%13.4f%12.6f',TT);
plot(true,x)
title('plot of f(x) to x')
xlabel('values of y')
ylabel('values of x')
grid
Note: I deliberately did not correct the problem with your first while loop. You are supposed to be constructing that table and plot for each x value in the range -0.95:0.05:0.95
6 Comments
Walter Roberson
on 13 Jul 2020
%xi = iteration
%ap = approximate value
%pe = percet error recorded
% pe desired 1*10^-5
fprintf('x Terms used approx Percent error\n');
fprintf('\n');
x = -0.95;
while x <= 0.95
% true = calc value of the function used.
true = log((1+x)/(1-x));
sumx=0;
xi = 0;
n = 0;
TT = zeros(4,0);
while(2)
xi = xi+1;
n = n+1;
sumx = sumx+(2*((x.^(2*n-1))/(2*n-1)));
% runs infinte series.
ap = sumx;
pe = abs((true-ap)/true)*100;
% above is error determination formula.
TT(:,end+1) = [x;xi;ap;pe];
if pe<=0.00001 || xi>= 200, break, end
% finalizes loop set up
fprintf('%2f%7d%13.4f%12.6f\n',TT);
end
plot(true,x, '*', 'displayname', sprintf('%.2f', x))
hold on
title('plot of f(x) to x')
xlabel('values of y')
ylabel('values of x')
grid
legend show
drawnow();
x = x + 0.05;
end
hold off
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!