How do i get the table to access all my iterative data?

3 views (last 30 days)
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

Answers (1)

Walter Roberson
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
andrew white
andrew white on 13 Jul 2020
perhaps i put it in wrong, but that ran into an error. I recall seeing an option that would put output data from a function into an array. If that were to follow while(1) would that be able to suplly a series of x values down the line properly. currently it runs x=0.95 until it stops for an unknown reason.
this is my current code:
clc;
%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(1)
x= x+0.05;
if x>=0.95, break, end
end
% 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)
title('plot of f(x) to x')
xlabel('values of y')
ylabel('values of x')
grid
I have been thinking about the plots and subplot issue myself. I'm trying to get the data table under control first because that is a more pressing matter. also, do you mean to say that becaus the script is running x as a scalar that the plot won't run successive values and instead a single dot?? my professor told me to make x a scalar for the script to run. i was originally using [-0.95:0.05:0.95] but it would cause a vertcat error due concatenation failure.
Walter Roberson
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

Sign in to comment.

Categories

Find more on Programming 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!