Simpson's rule for integration- finding area and centroid, program does not work.
Show older comments
I have written a program for finding the area and centroid for a mathematical function, unfortunately it does not seem to work, and i am getting various errors.
Here is my code in full: main-
function main()
global a b h n temp
a=(-0.5);
b=0.5;
n=100;
h=(b-a)/n;
[area, temp]= simpson(fun, fun2);
area=2*area;
Xc=(1/area)*temp;
disp(area)
disp(Xc)
end
Simpson's method, the action itself-
function [ area, temp] = simpson( fun, fun2 )
%Simpson's 1/3 integration method and calculating required values
global a b h
[se1, se2]=sumev(h);
[so1, so2]=sumod(h);
area= (h/3)*(fun(a)+fun(b)+(2*se1)+(4*so1));
temp= (h/3)*(fun2(a)+fun2(b)+(2*se2)+(4*so2));
end
summing even intervals-
function [ se1, se2] = sumev( h )
%Looping function for summing the values in even intervals for
%each function
global n
for j=0:2:n-2
v=(2*j+2)*h;
se1=se1+fun(v);
se2=se2+fun2(v);
end
summing odd intervals-
function [ so1, so2 ] = sumod( h )
%Looping function for summing the values in odd intervals for
%each function
global n
for j=0:2:n-1
v=(2*j+1)*h;
so1=so1+fun(v);
so2=so2+fun2(v);
end
The first function-
function [ q ] = fun(v)
%The mathematical function to integrate for the area
q=(2*(v^2)+0.2*v+0.08)*sqrt(1-4*(v^2));
end
Second function, used to find the centroid-
function [ e ] = fun2(v)
%The mathematical function to integrate for the area
e=v*(2*(v^2)+0.2*v+0.08)*sqrt(1-4*(v^2));
end
As far as i see, it's a very simple program, i don't see any missed letters or variable names, yet i'm getting these errors when i try to run main-
Error using fun (line 3) Not enough input arguments.
Error in main (line 8) [area, temp]= simpson(fun, fun2);
Any ideas and help debugging would be much appreciated
Accepted Answer
More Answers (0)
Categories
Find more on Assumptions 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!