Upload
cedric-dela-cruz
View
223
Download
2
Embed Size (px)
DESCRIPTION
Experiment No. 2 - Approximation and Round-Off Errors
Citation preview
Numerical Methods
Numerical Methods
Group No.:______________________________________Rating:__________________________
Date Performed: _________________________________Date Submitted: __________________
Numerical Methods
APPROXIMATION AND ROUND-OFF ERRORS
Experiment No. 2
I. OBJECTIVES
1. Develop algorithms for iterative methods in the approximation of values of functions using infinite series expansions.
2. Determine the true and approximate relative errors due to rounding-off for each iterative method, and establish a stopping criterion for each algorithm.
3. Evaluate the effect of the use of single and double precision floating-point numbers in the accuracy of the numerical solution.
II. MACHINE PROBLEMS
1. If it is known that
For , evaluate this series correct to seven significant figures. How many terms are required to achieve such accuracy?
2. Evaluate using two approaches
and
and compare with the true value of . Use 20 terms to evaluate the series and compute the true and relative errors as terms are added.
3. The infinite series
converges on a value of as approaches infinity. Evaluate this infinite series in single and double precision to calculate for by computing the sum from to and in reverse order. Also evaluate the true and approximate relative errors for each case.
4. Determine the roots of a quadratic equation having , , and . Use single and double precision arithmetic. Evaluate the true relative error and determine the cause of the discrepancy between the solutions. The true roots for the equation are and .
5. For 100 equally spaced values of between to , evaluate the expressions
and
Note that both expressions are mathematically equivalent. Using plotting tools, graph the function in the given interval. Which function is better in terms of resisting loss of significance?
III. METHODOLOGY
Machine Problem 2.1
Code 2.1.1 Pseudocode for Problem 2.1
x = 0.1
I=0
prev=0
Ea=100
If xEs Exit
pa = av
End Do
Display Ea, Et, av
Code 2.1.2 VBA Code for Problem 2.1
Sub machineproblem1()
Dim count As Double
Dim pa As Double
Dim av As Double
Dim tv As Double
Dim Ea As Double
Dim Et As Double
Dim Es As Double
count = 0
pa = 0
Ea = 100
Es = 0.5 * 10 ^ -5
x = 0.1
If Abs(x) < 1 Then
tv = 1 / (1 - x)
Et = ((tv - 1) / tv) * 100
Do While Abs(Et) > Es
av = pa + x ^ count
count = count + 1
Ea = ((av - pa) / av) * 100
Et = ((tv - av) / tv) * 100
Range("A" & count + 2) = count
Range("B" & count + 2) = av
Range("C" & count + 2) = Abs(Ea)
Range("D" & count + 2) = Abs(Et)
pa = av
Loop
MsgBox "Approximate Value is " & av
End If
End Sub
Code 2.1.3 MathScript Code for Problem 2.1
format long
x = 0.1;
if abs(x)Es
av = pa + x ^ count;
count = count + 1;
Ea = ((av - pa) / av) * 100;
Et = ((tv - av) / tv) * 100;
pa = av;
end
fprintf('Approximated Value is %f \n',av)
fprintf('Relative Error is %f percent \n',Ea)
fprintf('True Error is %f percent \n',Et)
fprintf('Number of Iteration %i iterations \n',count)
end
Machine Problem 2.2
Code 2.2.1 Pseudocode for Problem 2.2
x = 5
n = 20
DO
av = pa + (-1) ^ count * x ^ count / factorial(count)
count = count + 1
Ea = ((av - pa) / av) * 100
Et = ((tv - av) / tv) * 100
pa = av
IF count>n EXIT
END DO
Code 2.2.2 VBA Code for Problem 2.2
Sub machineproblem2()
Dim count1 As Double
Dim count2 As Double
x = 5
n = 20
count1 = 0
count2 = 0
tv = Exp(-x)
Range("B2").Value = tv
'Approach 1
Do While count1 < n
av = pa + (-1) ^ count1 * x ^ count1 / factorial(count1)
count1 = count1 + 1
Ea = ((av - pa) / av) * 100
Et = ((tv - av) / tv) * 100
Range("A" & count1 + 6) = count1
Range("B" & count1 + 6) = av
Range("C" & count1 + 6) = Abs(Ea)
Range("D" & count1 + 6) = Abs(Et)
pa = av
Loop
'Approach 2
Do While count2 < n
av2 = pa2 + x ^ count2 / factorial(count2 * 1)
count2 = count2 + 1
iav = 1 / av2
Ea2 = ((iav - ipa) / iav) * 100
Et2 = ((tv - iav) / tv) * 100
Range("F" & count2 + 6) = count2
Range("G" & count2 + 6) = iav
Range("H" & count2 + 6) = Abs(Ea2)
Range("I" & count2 + 6) = Abs(Et2)
ipa = iav
pa2 = av2
Loop
Range("B3").Value = Range("B26").Value
Range("B4").Value = Range("G26").Value
End Sub
Code 2.2.3 MathScript Code for Problem 2.2
APPROACH 1
y=5;
x=(1-y);
i=2;
table=ones(20,4);
true=.006737947;
while i>figure
>>subplot(2,2,1);
>> plot(x,y,'g');
>>title('sqrt(x+4)-sqrt(x+3)')
>>z=1./(sqrt(x+4)+sqrt(x+3));
>>subplot(2,2,2);
>> plot(x,z);
>>title('1/(sqrt(x+4)+sqrt(x+3))')
IV. RESULTS AND INTERPRETATION
Machine Problem 2.1 Power Series
VBA:
Figure 2.1.1 Once ran, the program will display the approximated values, the approximation errors and true errors in every iteration.
MATLAB:
Figure 2.1.2 Once ran, the program will display the approximated value, relative error, true error, and the number of iterations of the power series corrected with seven significant figures
Machine Problem 2.2
VBA:
Figure 2.2.1 Once ran, the program will display the data of the two approaches used in the program, with 20 iterations as stated in the problem.
MATLAB:
Figure 2.2.2 Once the mathscript file (MachineProblem2a) is ran in MATLAB, the program will display the 20 iterations with the corresponding approximate values, true error and relative error
Figure 2.2.3 For the second approach, once the mathscript file (MachineProblem2b) is ran in MATLAB, the program will display the 20 iterations with the corresponding approximate values, true error and relative error.
Machine Problem 2.3
VBA:
Figure 2.3.1 Once ran, the program will display the data in single and double data types in 10000 iterations.
Figure 2.3.2 Once ran, the program will display the data in single and double data types in 10000 iterations where k = 10000.
MATLAB:
Figure 2.3.3 Once the mathscript file (MachineProblem3a) is ran in MATLAB, the program will display the 10000 iterations with the corresponding approximate values, true error and relative error
Figure 2.3.4 For the reversed order, once the mathscript file (MachineProblem3b) is ran in MATLAB, the program will display the 10000 iterations with the corresponding approximate values, true error and relative error.
Machine Problem 2.4
VBA:
Figure 2.4.1 Once ran, the program will display the roots of the given parameters in single and double data types.
MATLAB:
Figure 2.4.2 Once the mathscript file (MachineProblem4) is ran in MATLAB, the program will display the roots of the equation in single and double data types.
Machine Problem 2.5
VBA:
Figure 2.5.1 - After configuring the x-scale of the graphs, the last step is labelling the axis, graphs, and the figure to relay informative data.
V. CONCLUSIONS AND RECOMMENDATIONS
Conclusion:
In the series of machine problem done. It was proven that problems involving Taylor series, Infinite Series, Loss of significance is easily done once ran using computer aided softwares such as MATLAB and Excel. And also data type plays a key role in numerical analysis. It was further proven in the Loss of Significance problem that number of decimal places or how precise the data affects the relative error of it.
Recommendations:
The students encountered problems in plotting data in Excel, specifically on how to employ through visual basic codes. The group recommends to the instructor to give background information on plotting in Excel and also on data types existing for data.
VI. REFERENCES
https://en.wikipedia.org/wiki/Loss_of_significance
https://msdn.microsoft.com/en-us/library/xay7978z.aspx
http://www.functionx.com/vbaexcel/Lesson03.htm
Machine Problem No.2 Approximation and Round-Off ErrorsPage 19