Experiment No. 1 - Development of Algorithm Using MS Excel and MATLAB
Experiment No. 1 - Development of Algorithm Using MS Excel and MATLAB
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.
0 to 100
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 ! .
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
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
Numerical Methods
END DO
DISPLAY result
END IF
Numerical Methods
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:
Numerical Methods
MATLAB:
Numerical Methods
MATLAB:
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
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
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.
MATLAB:
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
VI. REFERENCES
For VBA syntax:
www.excel-easy.com/vba.html
Numerical Methods
For MATLAB syntax:
http://www.mathworks.com/help/matlab/ref/.html