Absolute Error and Relative Error, Help please!!

So I've found one approximation of ln(x) to be 2* the sum of 1/(2n+1)*((x-1)/(x+1))^(2n+1) and I need to find the absolute and relative errors at every new x and n value and then graph them in a 3d Surface Plot.
Here's what I have so far, I'm not sure where to go from here:
%
function [abserror,relerror]=error_comparison(x,n)
sum1=0;
for i=0:n
sum1=sum1+ 1./(2.*i+1).*(((x-1)./(x+1)).^(2*i+1)); % an approximation of ln(x)
end
sum=2*sum1;
abserror= log(x)-sum
relerror=abserror./log(x)
end

9 Comments

Looks like you are taking the same course as Ping. This question was previously asked today.
I see. I think I have some code though, and just need some help debugging somewhere (which i've edited as well and gives me the right answers, but not all the values, I need 4 values, it outputs 2)
%
function [abserror,relerror]=error_comparison(x,n)
sum1=0;
for i=0:n
sum1=sum1+ 1./(2.*i+1).*(((x-1)./(x+1)).^(2*i+1)); % an approximation of ln(x)
end
sum=2*sum1;
abserror= log(x)-sum
relerror=abserror./log(x)
end
I'm not sure which 4 values you need to pass out, but you're only passing out two in [abserror,relerror] so just add the others.
I'm saying that if the inputs of x and n, are say, [0.5 1.5] and [1 2] respectively, I'm only getting two outputs instead of 4.
If you want 4 different outputs, you need to provide 4 different inputs. You are providing only 2, x=.5, n=1 and x=1.5, n=2. Those are only two cases. To provide 4 cases, such as you've used in your example,try
x=[.5 1.5 .5 1.5];
n=[1 2 2 1];
The outputs of the test case function [abserror,relerror]=error_comparison([0.5,1.5],[1 2]) are supposed to be abs error=
-1.7892e-03 1.3177e-04
1.4307e-04 3.7748e-06
relerror =
2.5812e-03 3.2500e-04
2.0640e-04 9.3097e-06
I'm only getting the first row of each, for some reason.
You're using n = [1,2] which is only two values. Use
x=[.5 1.5 .5 1.5];
n=[1 2 2 1];
to get all four cases.
Writing the negative exponent to give the same result as the calculator. Example: 0.3000 * 10^-3 gives a result of 3 * 10^-4
But in MATLAB it gives a very different result. What is the problem here or how to write the equation correctly to give the same value as the calculator
I don't know what you mean.
0.3000 * 10^-3
ans = 3.0000e-04
3 * 10^-4
ans = 3.0000e-04

Sign in to comment.

Answers (0)

Categories

Asked:

SB
on 2 Nov 2012

Commented:

on 7 Mar 2023

Community Treasure Hunt

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

Start Hunting!