Simpson's rule for integration, solving 2 problems in one go

3 views (last 30 days)
Hi everyone. I've been given a task to use simpson's rule (the 1/3) for integration in order to find the area AND the centroid of a certain 2 dimensional shape.
The area would be integrating from -0.5 to 0.5 The centroid would be integrating the function multiplied by x, and dividing by the area. (again, -0.5 to 0.5). My problem- every way i think of requires doing 2 sets of functions. one for the area, one for the centroid.
What i have: in main-
global z=-0.5 y=0.5 n=[number of intervals i choose] h=(z-y)/n
res=simpson(h)
Functions:
function[q]=fun(v)
global n
q=(2v^2+0.2v+0.08)*sqrt(1-4v^2)
end
function[a]=sumodd(h)
global n
for j=0:2:n-1
v=(2*j-1)*h
a=a+fun(v)
end
function[b]=sumev(h)
global n
for j=0:2:n-2
v=(2*j+2)*h
b=b+fun(v)
end
function[si]=simpson(h)
global z y
si= (h/3)*(fun(z)+fun(y)+2*sumev(h)+4*sumodd(h))
(Not written in matlab yet, so the syntax is not perfect) this takes care of finding the area. If anyone has any ideas how to find the centroid without building 2 extra summing functions (to call in fun2 instead of fun, where fun2 is the original function multiplied by x, or v in the code line) i would love to hear them. I need to keep this program as efficient as possible. Help is much appreciated. thanks

Answers (1)

John D'Errico
John D'Errico on 28 May 2015
Edited: John D'Errico on 28 May 2015
Is it bad that you have two functions? Modular code is FAR more easy to debug. As well when you write separate functions for these purposes, you can test each of them separately. Get each working on its own. It is also better in general, because in the future, one might not care about both returns.
(Aside) A problem is that as you learn to write code, if you get in the habit of making your code into one single behemoth, it will in fact become exactly that. For example, I had a friend long ago, who wrote famously bad spaghetti code. In fact, this was one of the people who taught me to write fortran. (Bad habits I had to unlearn.) He only had one fortran program. It did anything he wanted it to do. When he wanted to do something new, he added a goto step at the very beginning to jump to his new problem. He would compile and link the entire box of cards to do anything. In fact, once he used it to balance his checking account. Just add a new branch in there, and he could do anything.
However, if you insist, you can easily just add the computations for centroid after the area. Or, you could write the code to compute either, with a flag as input to the function to choose between the options.
Even so, I'd go with two distinct functions. It really is a better ay to write code. You can have one main function that calls each, and returns both results as return arguments. And nothing says that these TWO separate functions need to be in distinct files. They can be nested functions or sub-functions.
  3 Comments
Torsten
Torsten on 29 May 2015
You should try to arrange the function for Simpson's rule as
function [si] = simpson(fun,a,b,h)
where fun is the function handle for the function you are actually dealing with, a and b are the limits of integration and h is the step size.
Furthermore, I would integrate sumodd and sumeven into simpson (one function instead of three).
Best wishes
Torsten.
Rob Dunham
Rob Dunham on 29 May 2015
I appreciate the help with trying to improve the code efficiency- but it's like a that because i was asked to separate each calculation into a different function, though it is annoying and a lot harder to deal with in any way.
The only problem i am currently facing in the program is how to use the summing functions twice, once for each mathematical function, given the loop inside.
Also, a general question about how things in matlab work- notice what in simpson i have "fun(z)", but in the function called "fun" the variable is v. does matlab insert the argument properly, meaning z-->v or do the arguments inside and outside have to be the same exact variable?
In short-
Calling to function-
fun(z)
In the function-
function[a]=fun(v)
Will this work with the function using the value of z as v?

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!