experiment no. 1 - development of algorithm using ms excel and matlab

23
Numerical Methods Group No.:_______________3 ___________________ Rating:__________________________ Date Performed: ________06 / 26/ 15 ______________ 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. Machine Problem No.1 – Development of Algorithms using MS Excel VBA and MathScript Page 1

Upload: cedric-dela-cruz

Post on 13-Dec-2015

28 views

Category:

Documents


1 download

DESCRIPTION

Development of Algorithm Using MS Excel and MATLAB

TRANSCRIPT

Numerical Methods

Group No.:_______________3___________________ Rating:__________________________Date Performed: ________06 / 26/ 15______________ Date Submitted: ____06 / 29 / 15_____

Numerical Methods

DEVELOPMENT OF ALGORITHMS USING MS EXCEL VBA AND MATLABExperimentNo. 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 0 to 100 and returns a letter grade according to the following scheme:

3. The cosine function can be evaluated by the following infinite series:

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

Numerical Methods

cos x=1− x2

2!+ x

4

4 !− x

6

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

cos x=1− x2

2

cos x=1− x2

2!+ x

4

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

x i+1=x i+

ax i2

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 !.

III. METHODOLOGY

Machine Problem 1.1

Machine Problem No.1 – Development of Algorithms using MS Excel VBA and MathScript Page 2

Numerical Methods

Code 1.1.1 – Pseudocode for Problem 1.1Input 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.1Sub main()Dim x As Variantx = InputBox("Enter a Number: ")

If x >= 10 Theny: x = x - 5 If x < 50 Then MsgBox "The value of x is: " & x, vbOKOnly Else GoTo y

End IfElse If x < 5 Then MsgBox ("x is equal to 5") Else MsgBox ("x is equal to 7.5") End If

End IfEnd Sub

Code 1.1.3 – MathScript Code for Problem 1.1x = input ('Enter a number: '); if x>=10while x>50 x=x-5;end else if x<5

Machine Problem No.1 – Development of Algorithms using MS Excel VBA and MathScript Page 3

Numerical Methods

x=5;else x=7.5; end end disp(num2str(x))

Machine Problem 1.2

Code 1.2.1 – Pseudocode for Problem 1.2Input XIf X >= 90 & X <= 100 Then Display AElseIf X >= 80 & X < 90 Then Display BElseIf X >= 70 & X < 80 Then Display CElseIf X >= 60 & X < 70 Then Display DElse Display F

Code 1.2.2 – VBA Code for Problem 1.2Sub Macpro02()

Dim Grade As StringGrade = Range("A1").ValueIf 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 IfRange("B1").Value = result

End Sub

Code 1.2.3 – MathScript Code for Problem 1.2x = input('Enter Numeric Grade:');

Machine Problem No.1 – Development of Algorithms using MS Excel VBA and MathScript Page 4

Numerical Methods

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');

elsedisp('Invalid input. Numeric Grade must ');

end

Machine Problem 1.3

Code 1.3.1 – Pseudocode for Problem 1.3Subcode(factorial)INPUT n

factorial = 1DO FOR i = 1, n, 1factorial = factorial*iEND DODisplay factorial

INPUT x , n

DO FOR i = 0, n, 1cosx = prev + (-1)^n * x^(2*i) / factorial(2*i)display cosx

END DO

Code 1.3.2 – VBA Code for Problem 1.3Sub macpro01()Dim x, i As DoubleDim n As Long

x = Range("B3")n = Range("B4")i = 0real = Cos(x)Range("C5").Value = realDo While i < ny = prev + (-1) ^ i * x ^ (2 * i) / factorial(2 * i)

Machine Problem No.1 – Development of Algorithms using MS Excel VBA and MathScript Page 5

Numerical Methods

prev = yCells(i + 7, 3) = yCells(i + 7, 2) = i + 1i = i + 1

Loop

End Sub

Function factorial(n As Double) As Double

Dim ctr As DoubleDim result As Double

result = 1For ctr = 1 To nresult = result * ctrNext ctr

factorial = result

End Function

Code 1.3.3 – MathScript Code for Problem 1.3x = 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 1.4

Code 1.4.1 – Pseudocode for Problem 1.4

Machine Problem No.1 – Development of Algorithms using MS Excel VBA and MathScript Page 6

Numerical Methods

INPUT aIF a<0DISPLAY 0ELSE

TV = Sqr(a)es = 0.5 * 10 ^ -6

DOnew = (prev + a / prev) / 2b = TV - newIF b < es EXITprev = newEND DODISPLAY new

AV = newerror = ((TV - AV) / TV) * 100DISPLAY error

Code 1.4.2 – VBA Code for Problem 1.4Sub macpro4()Dim a, b, neww, prev, error, ea, es As Doublea = InputBox("Enter value of a")Range("B3").Value = aes = 0.5 * 10 ^ -6Range("B4").Value = Sqr(a)TV = Sqr(a)prev = 1b = 1i = 0Do While Abs(b) > esneww = (prev + a / prev) / 2b = TV - newwprev = newwi = i + 1LoopRange("B5").Value = newwAV = newwerror = ((TV - AV) / TV) * 100Range("B6").Value = errorRange("B7").Value = iEnd SubCode 1.4.3 – MathScript Code for Problem 1.4a = input('Enter value of a: ');if a <= 0

Machine Problem No.1 – Development of Algorithms using MS Excel VBA and MathScript Page 7

Numerical Methods

disp(0)elseformat shortTV = 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;endfprintf('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.5INPUT num

result = 1ctr = 1IF num<0

Display INVALID VALUEELSEIF num>0

DoFor 1, num, 1result = result * ctr

END DODISPLAY resultEND IF

Code 1.5.2 – VBA Code for Problem 1.5Sub factorial()Range("B3").Value = 0

Machine Problem No.1 – Development of Algorithms using MS Excel VBA and MathScript Page 8

Numerical Methods

Range("B4").Value = 0

Dim ctr As DoubleDim result As Doublenum = InputBox("Enter value of n:")Range("B3").Value = numIf num < 0 ThenMsgBox "Invalid Input", vbExclamationElseresult = 1For ctr = 1 To numresult = result * ctrNext ctrRange("B4").Value = resultprompt = "n! =" & resultMsgBox promptEnd IfEnd Sub

Code 1.5.3 – MathScript Code for Problem 1.5n = input('Enter value of n: ');if n<0 disp('Invalid Input')elseres=1;for i=1:n res = res*i;endfprintf('n! = %d\n',res)end

IV. RESULTS AND INTERPRETATION

Machine Problem 1.1

VBA:

Machine Problem No.1 – Development of Algorithms using MS Excel VBA and MathScript Page 9

Numerical Methods

MATLAB:

Machine Problem No.1 – Development of Algorithms using MS Excel VBA and MathScript Page 10

Numerical Methods

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:

MATLAB:

Machine Problem No.1 – Development of Algorithms using MS Excel VBA and MathScript Page 11

Numerical Methods

Machine Problem 1.3

VBA:

Machine Problem No.1 – Development of Algorithms using MS Excel VBA and MathScript Page 12

Numerical Methods

MATLAB:

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

Machine Problem No.1 – Development of Algorithms using MS Excel VBA and MathScript Page 13

Numerical Methods

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

MATLAB:

Machine Problem No.1 – Development of Algorithms using MS Excel VBA and MathScript Page 15

Numerical Methods

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

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

Machine Problem No.1 – Development of Algorithms using MS Excel VBA and MathScript Page 16

Numerical Methods

VBA:

MATLAB:

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

Machine Problem No.1 – Development of Algorithms using MS Excel VBA and MathScript Page 17

Numerical Methods

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

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 18