Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating Area Under the Graph Using Visual Basic 4

Status
Not open for further replies.

yliew

Civil/Environmental
Jan 16, 2002
19
AU
Dear All,

I am looking for a way to calculate the area under a non linear graph. I know it can be easily done using spreadsheet but are there any way which I can utilise Visual Basic to do all the calculation in the background and come out with one results.

I have:
~~~~~~~
1. Equation of the non linear graph (Load vs Deflection)
2. I known deflection.

(which means that once I input the deflection I will get the load)

My concept:
~~~~~~~~~~~
For discussion purposes, the non linear equation is:

Load = 0.85*(Displacement)^2

First of all, I determind the size of decrement of displacement which I want, say 0.1mm.

I will use the basic trapeziod and triangle method to find the area.

However, if I used the STEP command, it will come out with a list of results (Load) if I display it in the spreadsheet.

My question is that without it displaying on the spreadsheet, how do I continue with the calculation in Visual Basic and come out with a single answer at the end.

Hope that I didn't confuse all of you.

Regards,
YEN
 
Replies continue below

Recommended for you

Well, that's so simple that I wonder if...
Just sum up the single values calculated in each for loop into a variable and return the value of that variable...
prex
motori@xcalcsREMOVE.com
Online tools for structural design
 
Prex thanks for your quick reply but I am not sure if I understand what you mean. Could you please elaborate?

Sorry.

Regards,
YEN
 
This sounds like you are doing an definite integral. All calculus books should have a description of how to perform the calculation. However, there is another method for doing this calculation. Unfortunately, I can not remember the name of it right now. I will look through some of my old school stuff and post any findings. But in the mean time, try doing an internet search on definite integrals.

Good luck and keep us posted!
 
yliew:

what prex is saying is to set up a looping proceedure that will step through each value in your set. For example, if you wanted to calculate the area under the graph from 1mm to 10mm with a step of .1mm, then you would have something to the effect of:

Sub AreaUnderGraph()
Dim Load As Double, Displacement As Double
Dim Area As Double, MySum As Double
MySum = 0
Displacement = InputBox("Enter the deflection:")
For i = 0 To Displacement Step 0.1
Load = 0.85 * (i) ^ 2
'...Area under graph equation here...: Area = (...)
MySum = MySum + Area
Next i
Range("A1").Value = MySum
End Sub

Where you as the user would enter "10" in the input box.

As you can see, using the for/next loop calculates the area under the curve from a displacement of 0 to a user specified displacement at a step of 0.1 and enters the sum into cell A1.

If you need more help, let me know

Good Luck!

jproj
 
Many thanks to jproj and malone for contributing to the discussion. Jproj, what you have suggested is exactly what I need. Thanks!!!

Regards,
YEN
 
Why not do the calculus?

Eqn:
Load = 0.85*(Displacement)^2


I take it displacement is the variable:
C = 0.85 'a constant
x = displacement

y = Cx^2

To find the area under the curve:
/end
|
| Cx^2 dx
/start


Start is the starting point, say 1 and end is the end point say 10

Integrating the above equation leads to:

(C/3)(end^3 - start^3)

What jproj is doing is called a Rieman (sp?) sum and is a valid approach. But since the equation that you presented is rather simple, I would approach it using calculus. It has the advantages of being easy to code as well as being fast.


public function area(C as single, start as single, end as single)

area = (c/3)(end^3 - start^3)
end function


P.S. Correct me if I'm wrong it's been awhile since I used my calculus Troy Williams
fenris@hotmail.com
 
Thanks Troy. I have since use calculus to find the area under the graph. Thanks for your advice!

Regards,
YEN
 
yliew:

If you (or anyone else) have any need for it, I wrote a VBA program that uses Simpson's approximation to find the integral approximation of an equation between two finite numbers (Say the integral of Y = 0.85*X^2 from 1 to 3). Let me know and I'll post the code.

jproj
 
Jproj, thanks for the offer!!! Can you please email me your codes? My email address is:

yliew@civag.unimelb.edu.au

Thanks again!
 
JPROJ, SIMPSON's rule is exactly what I was thinking about.
 
Here is the procedure function code from the Simpson tool in the TK Solver math library. You could link your spreadsheet with TK and have it do the calculations for you. In this case, the best solution is just to do the calculus.

n is the number of function "slices" to evaluate.
a and b are the integration limits.
fun is the function definition.

h = (b-a)/n
x = a
k = 1
value = 0
for i = 2 to n
x = x + h
k = 3 - k
v = v + k*apply(fun,x)
next i
v = (2*v + apply(fun,a) + apply(fun,b))*h/3
 
Thanks TKsolver for your input!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top