Trapezoidal Rule in MATLAB.

  • Follow


Hi,

I'm trying to integrate the following function:
20/(1+x^2)
in MATLAB using the trapezoidal rule ( not using the Matlab function 'trapz').

I'm having two issues:

1). When i input 
MATLAB CODE:
N=11;
a= -2;
b=2;
h=(b-a)./N;
for  i =1:N-1;
 fsum=0.5*(20/(1+a^2) + 2*sum(20/(1+(a+i*h)^2)) + 20/(1+b^2))*h;
end

the 2*sum(20/(1+(a+i*h)^2)) term does not sum up. it just gives the first value.

I've tried just evaluating this term on it's own, but the sum function doesn't seem to be working. Does anyone know what i'm doing wrong. i've been staring at it for hours and can't seem to get it to work.

2) The second issue i'm having is in relation to trying to do the above with an M-file called funqc:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ y ] = funqc( x )
y = 20./(1+x.^2);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
I want to pass this into fsum so that it reads:
fsum=0.5*(@funqc(a) + 2*sum(@funqc(a+i*h))) + @funqc(b))*h;
However, i'm not too sure what to do with the a,a+i*h, and the b instead of the x in the function.

Should i create the loop in the M-file and then simply evaluate something along the lines of 'z=Funqc(a,d,N)?

Any help much appreciated.

Michael
0
Reply Michael 1/6/2011 1:26:05 AM

On 1/5/2011 5:26 PM, Michael Mouire wrote:
> Hi,
>

> I'm having two issues:
>
> 1). When i input
> MATLAB CODE:
> N=11;
> a= -2;
> b=2;
> h=(b-a)./N;
> for  i =1:N-1;
>   fsum=0.5*(20/(1+a^2) + 2*sum(20/(1+(a+i*h)^2)) + 20/(1+b^2))*h;
> end
>
> the 2*sum(20/(1+(a+i*h)^2)) term does not sum up. it just gives the first value.

To sum something you need

the_sum=0
LOOP
  the_sum = the_sum + stuff
END LOOP

You are not doing that.  How do you expect fsum to be added to in the loop?

--Nasser
0
Reply Nasser 1/6/2011 1:45:13 AM


"Michael Mouire" wrote in message <ig35nd$7fo$1@fred.mathworks.com>...
> Hi,
> 
> I'm trying to integrate the following function:
> 20/(1+x^2)
> in MATLAB using the trapezoidal rule ( not using the Matlab function 'trapz').
> 
> I'm having two issues:
> 
> 1). When i input 
> MATLAB CODE:
> N=11;
> a= -2;
> b=2;
> h=(b-a)./N;
> for  i =1:N-1;
>  fsum=0.5*(20/(1+a^2) + 2*sum(20/(1+(a+i*h)^2)) + 20/(1+b^2))*h;
> end
> 
> the 2*sum(20/(1+(a+i*h)^2)) term does not sum up. it just gives the first value.
> 
> I've tried just evaluating this term on it's own, but the sum function doesn't seem to be working. Does anyone know what i'm doing wrong. i've been staring at it for hours and can't seem to get it to work.
> 
> 2) The second issue i'm having is in relation to trying to do the above with an M-file called funqc:
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%
> function [ y ] = funqc( x )
> y = 20./(1+x.^2);
> end
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> I want to pass this into fsum so that it reads:
> fsum=0.5*(@funqc(a) + 2*sum(@funqc(a+i*h))) + @funqc(b))*h;
> However, i'm not too sure what to do with the a,a+i*h, and the b instead of the x in the function.
> 
> Should i create the loop in the M-file and then simply evaluate something along the lines of 'z=Funqc(a,d,N)?
> 
> Any help much appreciated.
> 
> Michael

sum works on a vector or matrix, all your values are scalars. (hint: don't need the for loop)
0
Reply proecsm 1/6/2011 1:48:05 AM

ah, i see.
when i remove the for loop from the equation, i get "error==>mpower".

Am i creating a 1 x n sized matrix with the results from fsum that can't be added to the others?


"proecsm" wrote in message <ig370l$4t$1@fred.mathworks.com>...
> "Michael Mouire" wrote in message <ig35nd$7fo$1@fred.mathworks.com>...
> > Hi,
> > 
> > I'm trying to integrate the following function:
> > 20/(1+x^2)
> > in MATLAB using the trapezoidal rule ( not using the Matlab function 'trapz').
> > 
> > I'm having two issues:
> > 
> > 1). When i input 
> > MATLAB CODE:
> > N=11;
> > a= -2;
> > b=2;
> > h=(b-a)./N;
> > for  i =1:N-1;
> >  fsum=0.5*(20/(1+a^2) + 2*sum(20/(1+(a+i*h)^2)) + 20/(1+b^2))*h;
> > end
> > 
> > the 2*sum(20/(1+(a+i*h)^2)) term does not sum up. it just gives the first value.
> > 
> > I've tried just evaluating this term on it's own, but the sum function doesn't seem to be working. Does anyone know what i'm doing wrong. i've been staring at it for hours and can't seem to get it to work.
> > 
> > 2) The second issue i'm having is in relation to trying to do the above with an M-file called funqc:
> > %%%%%%%%%%%%%%%%%%%%%%%%%%%%
> > function [ y ] = funqc( x )
> > y = 20./(1+x.^2);
> > end
> > %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> > I want to pass this into fsum so that it reads:
> > fsum=0.5*(@funqc(a) + 2*sum(@funqc(a+i*h))) + @funqc(b))*h;
> > However, i'm not too sure what to do with the a,a+i*h, and the b instead of the x in the function.
> > 
> > Should i create the loop in the M-file and then simply evaluate something along the lines of 'z=Funqc(a,d,N)?
> > 
> > Any help much appreciated.
> > 
> > Michael
> 
> sum works on a vector or matrix, all your values are scalars. (hint: don't need the for loop)
0
Reply Michael 1/10/2011 3:32:04 AM

On Jan 9, 10:32=A0pm, "Michael Mouire" <Michael.mou...@gmail.com> wrote:
> ah, i see.
> when i remove the for loop from the equation, i get "error=3D=3D>mpower".
>
> Am i creating a 1 x n sized matrix with the results from fsum that can't =
be added to the others?
>

When you remove the loop should i be a scalar or a vectot?

Hope this helps.

Greg
0
Reply Greg 1/10/2011 8:07:45 AM

4 Replies
747 Views

(page loaded in 0.052 seconds)

Similiar Articles:













7/12/2012 4:06:29 PM


Reply: