Anda di halaman 1dari 19

Numerical Methods

Group No.:_______________3___________________
Date Performed: ________06 / 26/ 15______________

Rating:__________________________
Date Submitted: ____06 / 29 / 15_____

Numerical Methods
DEVELOPMENT OF ALGORITHMS USING MS EXCEL VBA AND MATLAB
ExperimentNo. 1
I. OBJECTIVES
1. Develop algorithms for a given task using pseudocodes.
2. Implement the pseudocodes into a program using MS Excel VBA and MathScript of MATLAB
II. MACHINE PROBLEMS
1. Write a well-structured pseudocode to implement the flowchart depicted in the figure below.

2. Develop a well-structured function procedure that is passed a numeric grade from


and returns a letter grade according to the following scheme:

0 to 100

Machine Problem No.1 Development of Algorithms using MS Excel VBA and


MathScript
Page 1

Numerical Methods
3. The cosine function can be evaluated by the following infinite series:
cos x=1

x2 x4 x6
+ +
2! 4! 6!

Write an algorithm to implement this formula so that it computes and prints out the values of
cos x as each term in the series is added. In other words, compute and print in sequence the
values for
cos x=1
2

cos x=1

x
2

cos x=1

x x
+
2! 4!

up to the order term n of your choosing. Write the algorithm as a well-structured pseudocode.
4. The divide and average method, an old-time method for approximating the square root of any
positive number a can be formulated as
xi +
x i+1=

a
xi

Write a well-structured pseudocode for the implementation of this algorithm. It should do the
following:

Allow for a user input a .Check whether the user input is valid. Display 0 when the input
is 0 or not valid.
Set a value of tolerance. The tolerance must ensure that the answer is correct to six decimal
places
Implement the formula. Repeat the calculation until the answer is correct to six decimal places.
Display the answer.

5. Write a well-structured pseudocode that will compute the factorial of an input integer. Make sure
that the algorithm includes checks on valid values, as well as a correct output for 0 ! .

Machine Problem No.1 Development of Algorithms using MS Excel VBA and


MathScript
Page 2

Numerical Methods

III. METHODOLOGY
Machine Problem 1.1
Code 1.1.1 Pseudocode for Problem 1.1
Input x
If x >= 10 Then
Do
x = x 5
If x < 50 Exit
End Do
Else
If x < 5 Then
X = 5
Else
x = 7.5
End If
End If
Code 1.1.2 VBA Code for Problem 1.1
Sub main()
Dim x As Variant
x = InputBox("Enter a Number: ")
If x >= 10 Then
y: x = x - 5
If x < 50 Then
MsgBox "The value of x is: " & x, vbOKOnly
Else
GoTo y
End If
Else
If x < 5 Then
MsgBox ("x is equal to 5")
Else
MsgBox ("x is equal to 7.5")
End If
End If
End Sub
Code 1.1.3 MathScript Code for Problem 1.1
Machine Problem No.1 Development of Algorithms using MS Excel VBA and
MathScript
Page 3

Numerical Methods
x = input ('Enter a number: ');
if x>=10
while x>50
x=x-5;
end
else if x<5
x=5;
else x=7.5;
end
end
disp(num2str(x))
Machine Problem 1.2
Code 1.2.1 Pseudocode for Problem 1.2
Input X
If X >= 90 & X <= 100 Then
Display A
ElseIf X >= 80 & X < 90 Then
Display B
ElseIf X >= 70 & X < 80 Then
Display C
ElseIf X >= 60 & X < 70 Then
Display D
Else
Display F
Code 1.2.2 VBA Code for Problem 1.2
Sub Macpro02()
Dim Grade As String
Grade = Range("A1").Value
If Grade >= 90 & Grade <= 100 Then
result = "A"
ElseIf Grade >= 80 & Grade < 90 Then
result = "B"
ElseIf Grade >= 70 & Grade < 80 Then
result = "C"
ElseIf Grade >= 60 & Grade < 70 Then
result = "D"
Else
result = "F"
End If
Range("B1").Value = result
Machine Problem No.1 Development of Algorithms using MS Excel VBA and
MathScript
Page 4

Numerical Methods
End Sub
Code 1.2.3 MathScript Code for Problem 1.2
x = input('Enter Numeric Grade:');
if (x >= 90) && (x <= 100)
disp('Your grade is equivalent to A');
elseif (x >= 80) && (x < 90)
disp('Your grade is equivalent to B');
elseif (x >= 70) && (x < 80)
disp('Your grade is equivalent to C');
elseif (x >= 60) && (x < 70)
disp('Your grade is equivalent to D');
elseif (x < 60)
disp('Your grade is equivalent to F');
else
disp('Invalid input. Numeric Grade must ');
end
Machine Problem 1.3
Code 1.3.1 Pseudocode for Problem 1.3
Subcode(factorial)
INPUT n
factorial = 1
DO FOR i = 1, n, 1
factorial = factorial*i
END DO
Display factorial
INPUT x , n
DO FOR i = 0, n, 1
cosx = prev + (-1)^n * x^(2*i) / factorial(2*i)
display cosx
END DO
Code 1.3.2 VBA Code for Problem 1.3
Sub macpro01()
Dim x, i As Double
Dim n As Long
Machine Problem No.1 Development of Algorithms using MS Excel VBA and
MathScript
Page 5

Numerical Methods
x = Range("B3")
n = Range("B4")
i = 0
real = Cos(x)
Range("C5").Value
Do While i < n
y = prev + (-1) ^
prev = y
Cells(i + 7, 3) =
Cells(i + 7, 2) =
i = i + 1

= real
i * x ^ (2 * i) / factorial(2 * i)
y
i + 1

Loop
End Sub
Function factorial(n As Double) As Double
Dim ctr As Double
Dim result As Double
result = 1
For ctr = 1 To n
result = result * ctr
Next ctr
factorial = result
End Function
Code 1.3.3 MathScript Code for Problem 1.3
x = input('Enter value of x: ');
n = input('Enter number of terms for approximation: ');
realvalue = cos(x);
fprintf('real value = %f \n', real);
i=0;p=0;
disp('cos x approximation');
fprintf('n
value\n');
while i<n
y = p + (-1) ^ i * x ^ (2 * i) / factorial(2 * i);
p = y;
i=i+1;
fprintf('%d
%f\n',i,p);
end
Machine Problem No.1 Development of Algorithms using MS Excel VBA and
MathScript
Page 6

Numerical Methods

Machine Problem 1.4


Code 1.4.1 Pseudocode for Problem 1.4
INPUT a
IF a<0
DISPLAY 0
ELSE
TV = Sqr(a)
es = 0.5 * 10 ^ -6
DO
new = (prev + a / prev) / 2
b = TV - new
IF b < es EXIT
prev = new
END DO
DISPLAY new
AV = new
error = ((TV - AV) / TV) * 100
DISPLAY error
Code 1.4.2 VBA Code for Problem 1.4
Sub macpro4()
Dim a, b, neww, prev, error, ea, es As Double
a = InputBox("Enter value of a")
Range("B3").Value = a
es = 0.5 * 10 ^ -6
Range("B4").Value = Sqr(a)
TV = Sqr(a)
prev = 1
b = 1
i = 0
Do While Abs(b) > es
neww = (prev + a / prev) / 2
b = TV - neww
prev = neww
i = i + 1
Machine Problem No.1 Development of Algorithms using MS Excel VBA and
MathScript
Page 7

Numerical Methods
Loop
Range("B5").Value = neww
AV = neww
error = ((TV - AV) / TV) * 100
Range("B6").Value = error
Range("B7").Value = i
End Sub
Code 1.4.3 MathScript Code for Problem 1.4
a = input('Enter value of a: ');
if a <= 0
disp(0)
else
format short
TV = sqrt(a);
fprintf('True value for square root of a: %f\n',TV);
p=1;c=1;i=0;
es = 0.5*10^-6;
while c>es
nw = (p + a/p)/2;
b = TV - nw;
c=abs(b);
i = i+1;
p=nw;
end
fprintf('Using Divide and Average method \n');
fprintf('Approximated Value: %f\n', nw);
AV = nw;
error = ((TV - AV) / TV) * 100;
fprintf('Percentage error: %f\n', abs(error));
fprintf('Number of iterations: %d\n',i);
end

Machine Problem 1.5


Code 1.5.1 Pseudocode for Problem 1.5
INPUT num
result = 1
ctr = 1
IF num<0
Display INVALID VALUE
ELSEIF num>0
DoFor 1, num, 1
result = result * ctr
Machine Problem No.1 Development of Algorithms using MS Excel VBA and
MathScript
Page 8

Numerical Methods
END DO
DISPLAY result
END IF

Code 1.5.2 VBA Code for Problem 1.5


Sub factorial()
Range("B3").Value = 0
Range("B4").Value = 0
Dim ctr As Double
Dim result As Double
num = InputBox("Enter value of n:")
Range("B3").Value = num
If num < 0 Then
MsgBox "Invalid Input", vbExclamation
Else
result = 1
For ctr = 1 To num
result = result * ctr
Next ctr
Range("B4").Value = result
prompt = "n! =" & result
MsgBox prompt
End If
End Sub
Code 1.5.3 MathScript Code for Problem 1.5
n = input('Enter value of n: ');
if n<0
disp('Invalid Input')
else
res=1;
for i=1:n
res = res*i;
end
fprintf('n! = %d\n',res)
end

Machine Problem No.1 Development of Algorithms using MS Excel VBA and


MathScript
Page 9

Numerical Methods

IV. RESULTS AND INTERPRETATION


Machine Problem 1.1
VBA:

Machine Problem No.1 Development of Algorithms using MS Excel VBA and


MathScript
Page 10

Numerical Methods

MATLAB:

Once you run the program, it will only require 1 user-input value then it will implement the flowchart shown
in the problem section of this report. Here are some values, that may result when used in MATLAB.
Machine Problem 1.2
VBA:

Machine Problem No.1 Development of Algorithms using MS Excel VBA and


MathScript
Page 11

Numerical Methods

MATLAB:

Machine Problem 1.3


VBA:

Machine Problem No.1 Development of Algorithms using MS Excel VBA and


MathScript
Page 12

Numerical Methods

MATLAB:

Machine Problem No.1 Development of Algorithms using MS Excel VBA and


MathScript
Page 13

Numerical Methods

First, we run the program then it will ask for the user-input values

Then, we enter the specified values. For this example, we will try to approximate
cos(5) using 10 terms of the Taylor Series

After that, the program will display the real value as well as the approximated
values of the Taylor Series for every term until the nth term
From this we can analyze how the value of the approximation is getting closer to the exact value as we
increase the number of terms of consideration from the Taylor Series. Therefore, the accuracy of this
approximation depends mainly on the number of terms to consider. We should also take note of the
precision of the terms as we get closer to the real value.
Machine Problem 1.4
VBA:
Machine Problem No.1 Development of Algorithms using MS Excel VBA and
MathScript
Page 14

Numerical Methods

Machine Problem No.1 Development of Algorithms using MS Excel VBA and


MathScript
Page 15

Numerical Methods

MATLAB:

Run the program and it will prompt to input values. For this example, we try to use and invalid value which
is a negative number since the square root of a negative number is imaginary

In this case, the MATLAB will return a zero 0 value because this program is not concerned with imaginary
numbers

Machine Problem No.1 Development of Algorithms using MS Excel VBA and


MathScript
Page 16

Numerical Methods
Now, if we enter a valid input, the program will display the true value for its square root and the
approximated value using divide and average method also indicating the number of iterations before
correcting to six decimal places
From this analysis, we can also say that the accuracy of this approximation is dependent also on the
number of iterations to get accurate results. We also get almost 0% error which shows the preciseness of
this approximation method.

Machine Problem 1.5


VBA:

MATLAB:

Machine Problem No.1 Development of Algorithms using MS Excel VBA and


MathScript
Page 17

Numerical Methods

Once the program is running, MATLAB will prompt the user to input value for n

Then, the program will display the factorial of the number through looping structures

V. CONCLUSIONS AND RECOMMENDATIONS


Conclusion
The group therefore concludes that the use of programming languages or computer methods helped us
lessen the time spent in the implementation of solutions to some mathematical problems involving tedious
calculations requiring many iterations taking much time manually without the availability of fast, digital
computers. VBA and MATLAB also provided a better representation of the solutions to our problems which
eases the analysis of data; also, with the help of debugging tools of VBA and MATLAB to easily track
syntax and logical errors of the program. We have also learned from this activity the importance of having
knowledge on the correct syntax of your program for proper documentation, less error, and better
presentation.
Recommendations
A short brief discussion pre-requisite to the experiment proper regarding the differences in syntax of VBA
and MATLAB programming language might have lessen the time spent in doing the experiment because
most students may not be new to the MATLAB environment but are not well-familiarized with loop
statements, if-else statements, and case structures in MATLAB since we have focused on matrices and
matrix manipulation from our previous study of the course.

VI. REFERENCES
For VBA syntax:
www.excel-easy.com/vba.html

Machine Problem No.1 Development of Algorithms using MS Excel VBA and


MathScript
Page 18

Numerical Methods
For MATLAB syntax:

http://www.mathworks.com/help/matlab/ref/.html

Machine Problem No.1 Development of Algorithms using MS Excel VBA and


MathScript
Page 19

Anda mungkin juga menyukai