How do I create a while loop with this percent error function that will work?

10 views (last 30 days)
Consider the following infinite series: f(x)=2[ x+ x^3/3+ …….+ x^(2n-1)/(2n-1 )+ …… ]
This series represents the function: f(x)=ln〖( (1+x)/(1-x)〗 ) for -1 < x < +1
Implement this formula to evaluate the function from x=-0.95 to x=+0.95 with increments of 0.05. For each value of x, use just enough number of terms in the series such that the %error defined below is less than or equal to 1x10-5.
%error=abs( (true value-series approximation)/(true value) )×100
This is what I have so far, I just don't know how to get the function to run for multiple values and record those values: function [nt appr perr] = series(x,nterms) true = log((1+x)/(1-x)); sumx = 0; for n =1:nterms; nt(n) = n; sumx = sumx+(2*((x^(2*n-1))/(2*n-1))); appr(n) = sumx; perr(n) = abs((true-appr(n))/true)*100; end zz=[nt;appr;perr]; fprintf('No.terms app.value percent err\n'); fprintf('%7d %13.6f %12.6f\n',zz); end
  2 Comments
Cameron
Cameron on 12 Mar 2014
Here's my new code so far. I have identified the problem, but I do not know how to fix it. When I remove the "it" from line 20 the code works, but it then gives the wrong values for percent error. I'm not sure how to correct the problem.
  1. function [it,appro,peerr] = Armbrester(x)
  2. %it stands for iterations; tells me how many terms it has done
  3. %appro stands for approximation
  4. %appro is the calculation from the infinite series
  5. %peerr stands for percent error
  6. %Least possible peerr= 0.00001
  7. true = log((1+x)/(1-x));
  8. %true value is the calculation for the log function
  9. sumx = 0;
  10. it = 0;
  11. n = 0;
  12. while(1)
  13. it = it+1;
  14. n = n+1;
  15. sumx = sumx+(2*((x.^(2*n-1))/(2*n-1)));
  16. appro = sumx;
  17. peerr = abs((true-appro)/true)*100;
  18. if peerr <= 0.00001 | it >= 200, break, end
  19. end
  20. zz=[x;it;appro;peerr];
  21. fprintf('Value of x No. Terms App.Value Percent Err');
  22. fprintf('%2f %7d %13.4f %12.6f',zz);
  23. plot(appro,true)
  24. title('Plot of f(x) versus x')
  25. xlabel('Values of f(x)')
  26. ylabel('Values of x')
  27. grid
  28. end

Sign in to comment.

Answers (1)

Mischa Kim
Mischa Kim on 10 Mar 2014
Edited: Mischa Kim on 12 Mar 2014
Cameron, there are a couple of thing:
function outdata = my_series(xlim, dx, err)
x = xlim(1):dx:xlim(2);
outdata = zeros(length(x),4);
for ii = 1:length(x)
appr = 0;
true = log((1 + x(ii))/(1 - x(ii)));
jj = 1;
while abs(100*(true - appr)/true)>err
appr = appr + 2*x(ii)^(2*jj-1)/(2*jj-1);
jj = jj + 1;
end
outdata(ii,:) = [true appr 100*(true-appr)/true jj];
end
% fprintf('No.terms app.value percent err\n');
% fprintf('%7d %13.6f %12.6f\n',zz); end
  • series is a built-in function, use another function name.
  • Use two loops: one that steps through all your x-values, the second that finds the approximation for each x-value.
  • You could print your results. It's a little cleaner to return results as output of your function first. If you call the function using
outdata = my_series([-0.5 0.5], 0.05, 1e-5)
and without semi-colon at the end of the command, the array will be printed anyways.

Community Treasure Hunt

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

Start Hunting!