[minirant] wtf MATLAB, WTF!

eLiu

Diamond Member
Jun 4, 2001
6,407
1
0
WTF!

I ran the same file like 20 times... all producing the same wrong answer. You could say I re-ran the case out of boredom or something--no good reason really.

BUT on roughly the 21st run, BAM I get the right answer. I hadn't edited my function AT ALL. I'd stared at it intensely to try and see why I was getting crappy results, but I definitely didn't change anything.

WTF. How does that even happen?!

It's not like the numbers were even close. The wrong answer I was getting consistently was about -15.stuff. The right answer is about 1.99stuff.

MORAL OF THE STORY: If your function doesn't do what you want, run it A BUNCH MORE TIMES and maybe the computer gods will change it for you.

GAH
-Eric
 

purbeast0

No Lifer
Sep 13, 2001
52,850
5,724
126
1. do you have any kind of random number generator in your function?

2. there is a programming forum here.

3. PEBCAK
 

apac

Diamond Member
Apr 12, 2003
6,212
0
71
Originally posted by: purbeast0
1. do you have any kind of random number generator in your function?

2. there is a programming forum here.

3. PEBKAC

it's better that way.
 

sdifox

No Lifer
Sep 30, 2005
94,948
15,088
126
Dude, you are not running this on a Pentium 60 with the FDIV bug are you?
 

eLiu

Diamond Member
Jun 4, 2001
6,407
1
0
No there is 0 randomness in this algorithm. I'm not that stupid :p And it's a core2duo system. The original pentium FP bug was waaaaaaaaaaaay before "my time" (I'm 21 now).

Also it doesn't access any external variables. It's a function that takes 3 arguments and has 1 output. I'm applying the forward-euler finite differencing method to the van der Pol system of diff eqs... very elementary stuff. The only semi-cute part is shifting a design-parameter by i*epsilon to estimate the derivative.

What is PEBKAC?

And I know there's a programming forum, but I'm more whining than anything, so I figured OT is the place for that :p

Edit: Pointers is possible since I do have 3 arrays, but that would be entirely out of my control, since MATLAB doesn't have user-accessible pointers. That'd be really ass too since it's virtually unfix-able/undiagnosable by me.
 

TuxDave

Lifer
Oct 8, 2002
10,572
3
71
Clear all your variables? I find it weird that a deterministic function doesn't have deterministic outputs.
 

eLiu

Diamond Member
Jun 4, 2001
6,407
1
0
Originally posted by: TuxDave
Clear all your variables? I find it weird that a deterministic function doesn't have deterministic outputs.

That's what I'm saying. I don't know if I should be scared or annoyed here.

There aren't any vars to clear b/c it's a function file--functions in matlab get their own separate "workspace" and the vars within are created during the call and destroyed after it's done.

Besides, every variable is initialized to my desired starting value (usually 0) anyway, so even if the previous statement were false, it wouldn't matter.
 

eLiu

Diamond Member
Jun 4, 2001
6,407
1
0
Originally posted by: notposting
PEBKAC = Problem Exists Between Keyboard And Chair....aka the user.

No wai! I'm infallible! haha :D

hypn0tik, yeah I know. Esp this new version (2007a). I've had more weird problems with this than any other version of matlab. The other day one of my coworkers had a weird dimension mismatch error. After looking through the code, we found nothing so I asked her to run it again for kicks... and voila, it worked the 2nd time. Srsly.
 

hypn0tik

Diamond Member
Jul 5, 2005
5,867
2
0
Originally posted by: eLiu

hypn0tik, yeah I know. Esp this new version (2007a). I've had more weird problems with this than any other version of matlab. The other day one of my coworkers had a weird dimension mismatch error. After looking through the code, we found nothing so I asked her to run it again for kicks... and voila, it worked the 2nd time. Srsly.

Yeah. Every time I run something, I make sure to type 'clear' and then call the M file that has my code. Usually I end up with problems on my end (i.e. code is messed up). Once I get it working though, I get the same results for a given set of conditions.

I've heard that dealing with 3-D matrices in Matlab is a pain but thus far I haven't encountered any problems with it. I guess I haven't been doing crazy enough things with it.
 

randumb

Platinum Member
Mar 27, 2003
2,324
0
0
Originally posted by: eLiu
Originally posted by: notposting
PEBKAC = Problem Exists Between Keyboard And Chair....aka the user.

No wai! I'm infallible! haha :D

hypn0tik, yeah I know. Esp this new version (2007a). I've had more weird problems with this than any other version of matlab. The other day one of my coworkers had a weird dimension mismatch error. After looking through the code, we found nothing so I asked her to run it again for kicks... and voila, it worked the 2nd time. Srsly.

Yeah, I hate MATLAB. Besides buggy behavior, the interface is slow and the syntax is inconsistent. How did this become the standard tool in CS/Engineering?
 

dullard

Elite Member
May 21, 2001
25,054
3,408
126
I haven't used MATLAB much at all. But isn't it based on Maple? If so, Maple had a bad habit of forgetting to initialize variables even if you explicityly did it. Thus, I'd start looking here:
Originally posted by: eLiu
Besides, every variable is initialized to my desired starting value (usually 0) anyway, so even if the previous statement were false, it wouldn't matter.
Double check to see if it really is initializing to your desired starting value, or if it is using the last value from the previous iteration. As with any ODE solver, a change like that to your initial value would be expected to give results like what you are experiencing.
 

Born2bwire

Diamond Member
Oct 28, 2005
9,840
6
71
Originally posted by: randumb
Originally posted by: eLiu
Originally posted by: notposting
PEBKAC = Problem Exists Between Keyboard And Chair....aka the user.

No wai! I'm infallible! haha :D

hypn0tik, yeah I know. Esp this new version (2007a). I've had more weird problems with this than any other version of matlab. The other day one of my coworkers had a weird dimension mismatch error. After looking through the code, we found nothing so I asked her to run it again for kicks... and voila, it worked the 2nd time. Srsly.

Yeah, I hate MATLAB. Besides buggy behavior, the interface is slow and the syntax is inconsistent. How did this become the standard tool in CS/Engineering?

Beats researching algorithms and typing them up by hand believe me.
 

jiggahertz

Golden Member
Apr 7, 2005
1,532
0
76
Originally posted by: randumb
Originally posted by: eLiu
Originally posted by: notposting
PEBKAC = Problem Exists Between Keyboard And Chair....aka the user.

No wai! I'm infallible! haha :D

hypn0tik, yeah I know. Esp this new version (2007a). I've had more weird problems with this than any other version of matlab. The other day one of my coworkers had a weird dimension mismatch error. After looking through the code, we found nothing so I asked her to run it again for kicks... and voila, it worked the 2nd time. Srsly.

Yeah, I hate MATLAB. Besides buggy behavior, the interface is slow and the syntax is inconsistent. How did this become the standard tool in CS/Engineering?

Rapid prototyping and the ease of use for matrices/visualization/data manipulation.
 

jiggahertz

Golden Member
Apr 7, 2005
1,532
0
76
Originally posted by: eLiu
WTF!

I ran the same file like 20 times... all producing the same wrong answer. You could say I re-ran the case out of boredom or something--no good reason really.

BUT on roughly the 21st run, BAM I get the right answer. I hadn't edited my function AT ALL. I'd stared at it intensely to try and see why I was getting crappy results, but I definitely didn't change anything.

WTF. How does that even happen?!

It's not like the numbers were even close. The wrong answer I was getting consistently was about -15.stuff. The right answer is about 1.99stuff.

MORAL OF THE STORY: If your function doesn't do what you want, run it A BUNCH MORE TIMES and maybe the computer gods will change it for you.

GAH
-Eric

Post your code... I'm curious now.
 

eLiu

Diamond Member
Jun 4, 2001
6,407
1
0
The code is attached.

Ebar is the output--an energy measure.

beta controls damping and the offset from the origin. plotflag tells it whether or not you want to generate plots & draw up the eigenvalues of the jacobian for each iteration.

The standard call is to do something like vdp2(1,1). To calculate derivative information of the output with respect to beta, you can employ the following trick: vdp2(1+i*1e-8,0). The imaginary part of that result will be approximately i*1e-8*\frac{dEbar}{d\beta}, with accuracy O(epsilon^2) w/epsilon = 1e-8.

The imaginary stuff is where I started having problems. The correct answer is roughly 2. For a while, I was getting -15. ><

Anyway, as of now MATLAB 7R14 and 2007a are both giving the right result:
>> vdp2(1+1e-8*sqrt(-1),0)
ans =
0.99920525703928 + 0.00000001994173i

dullard, MATLAB is separate from Maple. MATLAB does have a symbolic algebra package that is based on Maple though. And yeah, I definitely understand where you're coming from b/c not being careful enough with initializations has definitely messed me up before.

randumb, I really love MATLAB, but every so often it annoys me. Yeah I know the interface can be crappy, which is why I run it in cmdline mode (linux) :)

LordMorpehus, BAH! :p I'm tellin you I didn't change anything... lol. I even did a "ls -l" in that directory to make sure!

edit: attaching code removed all my linebreaks :confused; so here it is:
function Ebar=vdp2(beta,plotflag)
uoffset = [beta;beta];
epsilon = 0.01;

dt = 0.01;
Tend = 100;

f = zeros(2,1);
f_v = zeros(2,2);

n = Tend/dt + 1;
t = zeros(1,n);
u = zeros(2,n);
u:),1) = [beta;beta]*1.00001;

for j=1:n-1
v0 = (1/epsilon)*(u:),j) - uoffset);

f(1) = v0(2);
f(2) = beta*(1-v0(1)^2)*v0(2) - v0(1);

du = epsilon*f*dt;
u:),j+1) = u:),j) + du;

t(j+1) = t(j) + dt;
end

E = 0.5*(u(1,:).^2 + u(2,:).^2);
Ebar = mean(E(round(n/2):n));

if plotflag
figure;
plot(u(1,:),u(2,:));
xlabel('u_1'); ylabel('u_2');

lambda = zeros(n,size(f_v,1));
for j = 1:n-1,
v0 = (1/epsilon)*(u:),j) - uoffset);
f_v(1,1) = 0;
f_v(1,2) = 1;
f_v(2,1) = beta*(-2*v0(1) )*v0(2) - 1;
f_v(2,2) = beta*(1-v0(1)^2);

lambda(j,:) = eig(f_v)';
end

figure
subplot(311);
plot(t,u(1,:));
paxis = axis;paxis(2) = Tend;axis(paxis);
xlabel('t');ylabel('u_1');
subplot(312);
plot(t,u(2,:));
paxis = axis;paxis(2) = Tend;axis(paxis);
xlabel('t');ylabel('u_2');
subplot(313);
plot(t,max(real(lambda),[],2));
paxis = axis;paxis(2) = Tend;axis(paxis);
xlabel('t');ylabel('max(\lambda_{real})');
end
 

randumb

Platinum Member
Mar 27, 2003
2,324
0
0
Originally posted by: jiggahertz
Originally posted by: randumb
Originally posted by: eLiu
Originally posted by: notposting
PEBKAC = Problem Exists Between Keyboard And Chair....aka the user.

No wai! I'm infallible! haha :D

hypn0tik, yeah I know. Esp this new version (2007a). I've had more weird problems with this than any other version of matlab. The other day one of my coworkers had a weird dimension mismatch error. After looking through the code, we found nothing so I asked her to run it again for kicks... and voila, it worked the 2nd time. Srsly.

Yeah, I hate MATLAB. Besides buggy behavior, the interface is slow and the syntax is inconsistent. How did this become the standard tool in CS/Engineering?

Rapid prototyping and the ease of use for matrices/visualization/data manipulation.

There are plenty of other tools available that are better IMO. In bioinformatics research for example, R has become extremely popular.
 

eLiu

Diamond Member
Jun 4, 2001
6,407
1
0
Originally posted by: randumb
Originally posted by: jiggahertz
Originally posted by: randumb
Originally posted by: eLiu
Originally posted by: notposting
PEBKAC = Problem Exists Between Keyboard And Chair....aka the user.

No wai! I'm infallible! haha :D

hypn0tik, yeah I know. Esp this new version (2007a). I've had more weird problems with this than any other version of matlab. The other day one of my coworkers had a weird dimension mismatch error. After looking through the code, we found nothing so I asked her to run it again for kicks... and voila, it worked the 2nd time. Srsly.

Yeah, I hate MATLAB. Besides buggy behavior, the interface is slow and the syntax is inconsistent. How did this become the standard tool in CS/Engineering?

Rapid prototyping and the ease of use for matrices/visualization/data manipulation.

There are plenty of other tools available that are better IMO. In bioinformatics research for example, R has become extremely popular.

What would you suggest for working with ODEs (ivp & bvp) or linear algebra? I mean there's Octave, but it isn't much different... ODEs in Maple can be pretty awkward (not to mention slow), and linalg is even worse. You could fire up FORTRAN77 and link to LAPACK, but I mean come on, that's a lot more work than opening matlab and typing eig().