Matlab Function for Composite Simpson

AI Thread Summary
The discussion centers on troubleshooting a MATLAB function for composite Simpson's rule, specifically addressing errors in the while loop and the calculation of the integral. Users highlight that the use of the "sum" function is appropriate since it operates on arrays generated by the variable 'i'. A key point raised is that the function should return the computed integral value as 'I', rather than storing it in 'S'. The original poster identifies a typo as the source of their errors, and after correcting it, the function now operates correctly. Overall, the conversation emphasizes the importance of proper function structure and syntax in MATLAB coding.
ver_mathstats
Messages
258
Reaction score
21
Homework Statement
We need to evaluate an integral sin(1/x) where b = 1 and a = 0.1 using composite Simpson in Matlab for n = 2, 4, 8... until the absolute error is within 10^-4, then we are required to apply the adaptive Simpson rule and compare the two. The program needs to output the number and error.
Relevant Equations
composite simpson rule
Matlab:
function I=main_simpson(a,b,tol)
f = @(x) sin(1./x);
SO = 0;
N = 10;
S = 1;
while (abs(S-SO)>tol)
    SO = S;
    h = (b-a)/(2*N);
    i = 0:N-1;
    xi = a+2*i*h;
    xi1 = a+2*(i+0.5)*h;
    xi2 = a+2*(i+1)*h;
    S = (h/3)*sum(f(xi)+4*f(xi1)+f(xi2));
    N = 2*N;
end
end
<Moderator's note: please use CODE tags when posting code.>

I keep experiencing errors when trying to use my composite Simpson code, could anyone tell me how I can improve this? The error is always coming from the while loop but I'm really unsure why and I do not know how to proceed or nothing happens when I input data into my function so I am really confused. Any help is appreciated.
 
Last edited by a moderator:
Physics news on Phys.org
ver_mathstats said:
S = (h/3)*sum(f(xi)+4*f(xi1)+f(xi2));
Why is "sum" there on the right side of the assignment? Matlab isn't my strong suit, but I believe that sum() is intended to be used on arrays.

The above should be written as S = (h/3) * (f(xi)+4*f(xi1)+f(xi2));, I believe.
ver_mathstats said:
I keep experiencing errors when trying to use my composite Simpson code, could anyone tell me how I can improve this? The error is always coming from the while loop but I'm really unsure why and I do not know how to proceed or nothing happens when I input data into my function so I am really confused.
What sort of errors? If you're getting error messages that possibly include the line number where the error occurred, that's useful information that should be included.
 
You have
Matlab:
function I=main_simpson(a,b,tol)
So the function will return the value of I, which is not what you calculate. You calculate the integral into S.

Note also that you do not follow what the question asks with regards to the number of points for integration at each attempt.

Mark44 said:
Why is "sum" there on the right side of the assignment? Matlab isn't my strong suit, but I believe that sum() is intended to be used on arrays.
In the code snippet below, the variable i is set as an array of integers, so xi, xi1 and xi2 will be arrays, as well as f(xi) and so on.
Matlab:
    i = 0:N-1;
    xi = a+2*i*h;
    xi1 = a+2*(i+0.5)*h;
    xi2 = a+2*(i+1)*h;
    S = (h/3)*sum(f(xi)+4*f(xi1)+f(xi2));
 
DrClaude said:
You have
Matlab:
function I=main_simpson(a,b,tol)
So the function will return the value of I, which is not what you calculate. You calculate the integral into S.

Note also that you do not follow what the question asks with regards to the number of points for integration at each attempt.In the code snippet below, the variable i is set as an array of integers, so xi, xi1 and xi2 will be arrays, as well as f(xi) and so on.
Matlab:
    i = 0:N-1;
    xi = a+2*i*h;
    xi1 = a+2*(i+0.5)*h;
    xi2 = a+2*(i+1)*h;
    S = (h/3)*sum(f(xi)+4*f(xi1)+f(xi2));
Yes I realized it was such a silly typo causing issues which is my mistake, I fixed it and now it works
 
Thread 'How do I determine the resistance for RLC low pass filter?'
Hi, I am trying to build a RLC low pass filter that atenuates the frequency below 4500 Hz. However, I have encountered some problem when choosing the correct R to work with. Here is the Circuit Here is the original sound. Here is my code in Matlab function Vout = myFilterCircuit(Vin,h) n_V = length(Vin); f_7 = 4470;; % Undesired frequency h_7 = h; % delta time % These are for the constant and initialization of the variables t_7 = 0:h_7:(n_V-1)*h_7; % This is the independent variable...
Thread 'Have I solved this structural engineering equation correctly?'
Hi all, I have a structural engineering book from 1979. I am trying to follow it as best as I can. I have come to a formula that calculates the rotations in radians at the rigid joint that requires an iterative procedure. This equation comes in the form of: $$ x_i = \frac {Q_ih_i + Q_{i+1}h_{i+1}}{4K} + \frac {C}{K}x_{i-1} + \frac {C}{K}x_{i+1} $$ Where: ## Q ## is the horizontal storey shear ## h ## is the storey height ## K = (6G_i + C_i + C_{i+1}) ## ## G = \frac {I_g}{h} ## ## C...
Back
Top