Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Problem with a variable not changing value in Matlab

Subject: Problem with a variable not changing value in Matlab

From: nisse

Date: 4 Jun, 2011 19:23:02

Message: 1 of 11

Hey, So I have one problem with my code, which is that one variable is not working as it's supposed to do. Here are the codes I'm using:

format long

f = inline('-x.^2');

for i = 0:10

    [I(i+1) h(i+1) tid(i+1)] = trapets(f,0,1,2^i);
end
for i = 0:10

 trunk(i+1) = I(i+1) - log(2);
end

hold on

grid on

plot(log(h),log(trunk),'r+')

t = -7:0;

c = polyfit(log(h),log(trunk),1);

yy = polyval(c,t);

plot(t,yy)

grid off

hold off

koefficienter = real(c)

and also this :

function [ I,h,tid ] = trapets( f,a,b,n )

h=(b-a)/n;

tic;

I=(f(a)+f(b));

for k=2:2:n-2

I = I+2*f(a+k*h);
end

for k = 1:2:n-1

I = I + 4*f(a+k*h);
end

I = I * h/3;

tid = toc;

end

So the problem here is that the variable 'I' is not changing value. It gets 11-values when I run the first code (I don't run the last code I wrote, it's only used by the first one), but the values are all the same. I don't know if the problem is that the variable 'n', which I use in the the second code, never change value, although I'm trying to do that with the "2^i" part in "trapets(f,0,1,2^i)". If the case is that 'n' never change value, does anyone know a solution do that? And if the problem is not the variable 'n', does anyone know why the variable 'I' doesn't change value in these codes??

I would really appreciate the help! Would save my week!

Subject: Problem with a variable not changing value in Matlab

From: ImageAnalyst

Date: 4 Jun, 2011 19:40:27

Message: 2 of 11

Because h is always 1/2^n, and your I always sums up to -2^n, so your
final I is always -1/3.

Subject: Problem with a variable not changing value in Matlab

From: nisse

Date: 4 Jun, 2011 19:55:07

Message: 3 of 11

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <ebf7dc98-542c-42e8-ae28-305007cb5454@y19g2000yqk.googlegroups.com>...
> Because h is always 1/2^n, and your I always sums up to -2^n, so your
> final I is always -1/3.

But the h is always 1/n and the n should be changing since I'm looping n = 2^i for i = 0:10 ?

Subject: Problem with a variable not changing value in Matlab

From: Roger Stafford

Date: 4 Jun, 2011 20:15:05

Message: 4 of 11

"nisse" wrote in message <ise0mm$e2d$1@newscl01ah.mathworks.com>...
> Hey, So I have one problem with my code, which is that one variable is not working as it's supposed to do. Here are the codes I'm using:
>
> format long
>
> f = inline('-x.^2');
>
> for i = 0:10
>
> [I(i+1) h(i+1) tid(i+1)] = trapets(f,0,1,2^i);
> end
> for i = 0:10
>
> trunk(i+1) = I(i+1) - log(2);
> end
>
> hold on
>
> grid on
>
> plot(log(h),log(trunk),'r+')
>
> t = -7:0;
>
> c = polyfit(log(h),log(trunk),1);
>
> yy = polyval(c,t);
>
> plot(t,yy)
>
> grid off
>
> hold off
>
> koefficienter = real(c)
>
> and also this :
>
> function [ I,h,tid ] = trapets( f,a,b,n )
>
> h=(b-a)/n;
>
> tic;
>
> I=(f(a)+f(b));
>
> for k=2:2:n-2
>
> I = I+2*f(a+k*h);
> end
>
> for k = 1:2:n-1
>
> I = I + 4*f(a+k*h);
> end
>
> I = I * h/3;
>
> tid = toc;
>
> end
>
> So the problem here is that the variable 'I' is not changing value. It gets 11-values when I run the first code (I don't run the last code I wrote, it's only used by the first one), but the values are all the same. I don't know if the problem is that the variable 'n', which I use in the the second code, never change value, although I'm trying to do that with the "2^i" part in "trapets(f,0,1,2^i)". If the case is that 'n' never change value, does anyone know a solution do that? And if the problem is not the variable 'n', does anyone know why the variable 'I' doesn't change value in these codes??
>
> I would really appreciate the help! Would save my week!
- - - - - - - - -
  This is Simpson's integration approximation. It is exact for polynomials of degree up to three, so your -x^2 function should give exact results no matter what n is. Trying using -x^4.

Roger Stafford

Subject: Problem with a variable not changing value in Matlab

From: nisse

Date: 4 Jun, 2011 20:30:20

Message: 5 of 11


> - - - - - - - - -
> This is Simpson's integration approximation. It is exact for polynomials of degree up to three, so your -x^2 function should give exact results no matter what n is. Trying using -x^4.
>
> Roger Stafford


Thanks so much!!

Since you know the math, do you know why the truncation can be found with I - log(2)?

Subject: Problem with a variable not changing value in Matlab

From: nisse

Date: 4 Jun, 2011 20:33:04

Message: 6 of 11


> - - - - - - - - -
> This is Simpson's integration approximation. It is exact for polynomials of degree up to three, so your -x^2 function should give exact results no matter what n is. Trying using -x^4.
>
> Roger Stafford


Thanks so much!!

Since you know the math, do you know why the truncation can be found with I - log(2)?

Subject: Problem with a variable not changing value in Matlab

From: Roger Stafford

Date: 4 Jun, 2011 23:27:02

Message: 7 of 11

"nisse" wrote in message <ise4ks$mso$1@newscl01ah.mathworks.com>...
> Thanks so much!!
> Since you know the math, do you know why the truncation can be found with I - log(2)?
- - - - - - - - -
  I recognize your 'trapets" as computing Simpson's integration approximation, but I have no idea why you subtract log(2) from it. Perhaps you could tell us why you do.

Roger Stafford

Subject: Problem with a variable not changing value in Matlab

From: ImageAnalyst

Date: 4 Jun, 2011 23:59:14

Message: 8 of 11

On Jun 4, 3:55 pm, "nisse " <knase...@hotmail.com> wrote:
> ImageAnalyst <imageanal...@mailinator.com> wrote in message <ebf7dc98-542c-42e8-ae28-305007cb5...@y19g2000yqk.googlegroups.com>...
> > Because h is always 1/2^n, and your I always sums up to -2^n, so your
> > final I is always -1/3.
>
> But the h is always 1/n and the n should be changing since I'm looping n = 2^i for i = 0:10 ?

------------------------------------------------------------------------
True, and it does. h is 1/n but that is 1 over a power of two
(because you're passing in 2^i) and that always seems to be the
inverse of your I so that when you multiply I by h/3 you're always
getting -1/3 no matter what.

Subject: Problem with a variable not changing value in Matlab

From: nisse

Date: 5 Jun, 2011 10:27:04

Message: 9 of 11

"Roger Stafford" wrote in message <isef06$h0g$1@newscl01ah.mathworks.com>...
> "nisse" wrote in message <ise4ks$mso$1@newscl01ah.mathworks.com>...
> > Thanks so much!!
> > Since you know the math, do you know why the truncation can be found with I - log(2)?
> - - - - - - - - -
> I recognize your 'trapets" as computing Simpson's integration approximation, but I have no idea why you subtract log(2) from it. Perhaps you could tell us why you do.
>
> Roger Stafford


Actually, I'm calculating the truncation with the subtraction of log(2), or anyway that's the tip I got from some of my classmates. I don't know why really, but it will probably work out anyway. Thanks for the help!

Subject: Problem with a variable not changing value in Matlab

From: nisse

Date: 5 Jun, 2011 14:13:05

Message: 10 of 11

"nisse" wrote in message <isfllo$fbq$1@newscl01ah.mathworks.com>...
> "Roger Stafford" wrote in message <isef06$h0g$1@newscl01ah.mathworks.com>...
> > "nisse" wrote in message <ise4ks$mso$1@newscl01ah.mathworks.com>...
> > > Thanks so much!!
> > > Since you know the math, do you know why the truncation can be found with I - log(2)?
> > - - - - - - - - -
> > I recognize your 'trapets" as computing Simpson's integration approximation, but I have no idea why you subtract log(2) from it. Perhaps you could tell us why you do.
> >
> > Roger Stafford
>
>
> Actually, I'm calculating the truncation with the subtraction of log(2), or anyway that's the tip I got from some of my classmates. I don't know why really, but it will probably work out anyway. Thanks for the help!


Alright, I now know that the I - log(2) is the truncation since log(2) is the actuall value of the integration of function 1/(1+x) over the interval 0<x<1. Subtract the actual value from the value one got from Simpsons approximation, and you get the truncation.

Subject: Problem with a variable not changing value in Matlab

From: Roger Stafford

Date: 5 Jun, 2011 15:08:02

Message: 11 of 11

"nisse" wrote in message <isg2th$fvs$1@newscl01ah.mathworks.com>...
> Alright, I now know that the I - log(2) is the truncation since log(2) is the actuall value of the integration of function 1/(1+x) over the interval 0<x<1. Subtract the actual value from the value one got from Simpsons approximation, and you get the truncation.
- - - - - - -
  I speculated that '1/(1+x)' was the function you were actually using. You shouldn't expect Simpson's rule to get the exact answer with this function.

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us