MATLAB to Python?

P

Peter Otten

MATLABdude said:
Can you, please, try to help me with Python? I try to convert a MATLAB
program to Python.

Here are the MATLAB codes:
http://pastebin.com/eJetYizv
http://pastebin.com/0eXTVfyN

Here is my Python code:
http://pastebin.com/jCPdLHx7

That is too much code for me, in a problem domain I'm not familiar with.
What is wrong with my Python code? The program doesn't produce quite
the same lambdas as the MATLAB program does. My code probably has lots
of errors because I'm just learning the basics about Python.

If no-one fixes it for you I suggest that you break the code in smaller
functions (at most ten lines) that you can test separately. That should make
it easier to spot the misbehaving parts of your calculation.
If you come back then with one of these, the matlab equivalent, sample input
and expected output a significantly larger number of people can and may be
willing to help.

Peter
 
C

Carl Banks

Hi!

Can you, please, try to help me with Python? I try to convert a MATLAB
program to Python.

Here are the MATLAB codes:http://pastebin.com/eJetYizvhttp://pastebin.com/0eXTVfyN

Here is my Python code:http://pastebin.com/jCPdLHx7

What is wrong with my Python code? The program doesn't produce quite
the same lambdas as the MATLAB program does. My code probably has lots
of errors because I'm just learning the basics about Python.

Thanks for all the help!

I was about to say it was a pretty good looking piece of code, and
probably if anything is wrong it's a typo somewhere.

But then my eye caught something at the last minute:

xx = range(-kappa, h, kappa+1)

Looks like you're treating range arguments as (start,step,stop), same
order as in Matlab. In Python it's (start,stop,step), so you should
be using this:

xx = range(-kappa,kappa+1,h)

Other than that, you might jus have to pepper your scripts with prints
and disps to catch the exact point where the discrepancy occurs.


Carl Banks
 
A

Arnaud Delobelle

MATLABdude said:
Hi!

Can you, please, try to help me with Python? I try to convert a MATLAB
program to Python.

Here are the MATLAB codes:
http://pastebin.com/eJetYizv
http://pastebin.com/0eXTVfyN

Here is my Python code:
http://pastebin.com/jCPdLHx7

What is wrong with my Python code? The program doesn't produce quite
the same lambdas as the MATLAB program does. My code probably has lots
of errors because I'm just learning the basics about Python.

Thanks for all the help!

I don't understand what's going on in your code, but line 81 you have:

xx = range(-kappa, h, kappa+1)

I guess that the step is supposed to be h, so you should write:

xx = range(-kappa, kappa+1, h)
 
M

MATLABdude

Thanks guys!

I still have some problems. I made the code somewhat simpler by
dropping off some parts of it (xx, f).

Now my Python code calculates 8 different values for lambda. The
problem is that I don't know where the minus sign comes from. Also the
values themselves are not identical compared to the values of the
MATLAB program.
nonhomog.py | nonhomog.m
-0.774244159818 | 0.9976
-0.823595831818 | 0.9969
-0.774374006419 | 0.5464
-1.0 | -54.3440
-0.774244172803 | 0.9976
-0.774244289666 | 0.9976
-0.823595831818 | 0.9969
-1.0 | 0.9674

The new Python code can be found here: http://pastebin.com/c1UvtXxu
 
M

MRAB

Thanks guys!

I still have some problems. I made the code somewhat simpler by
dropping off some parts of it (xx, f).

Now my Python code calculates 8 different values for lambda. The
problem is that I don't know where the minus sign comes from. Also the
values themselves are not identical compared to the values of the
MATLAB program.
nonhomog.py | nonhomog.m
-0.774244159818 | 0.9976
-0.823595831818 | 0.9969
-0.774374006419 | 0.5464
-1.0 | -54.3440
-0.774244172803 | 0.9976
-0.774244289666 | 0.9976
-0.823595831818 | 0.9969
-1.0 | 0.9674

The new Python code can be found here: http://pastebin.com/c1UvtXxu

Don't use:

from math import *
from numpy import *
from scipy import *

because you'll never be sure where a name comes from!

Also, check for accidental integer division. (In Python 2 integer /
integer returns integer.)
 
M

MATLABdude

I guess that the step is supposed to be h, so you should write:
    xx = range(-kappa, kappa+1, h)

This is what I have in the source code:
---8<---8<---8<---8<---
h = 0.105069988414
xx = range(-kappa, kappa+1, h)
---8<---8<---8<---8<---

This is what Python says: ValueError: range() step argument must not
be zero

Can step not be a float value?
 
C

Chris Rebert

This is what I have in the source code:
---8<---8<---8<---8<---
h =  0.105069988414
xx = range(-kappa, kappa+1, h)
---8<---8<---8<---8<---

This is what Python says: ValueError: range() step argument must not
be zero

Can step not be a float value?

Correct. Note the DeprecationWarning which is also shown:
__main__:1: DeprecationWarning: integer argument expected, got float

There are too many subtleties with floating-point, so range() doesn't
handle it; that way, you deal with the subtleties yourself,
explicitly.

Cheers,
Chris
 
P

Peter Otten

MATLABdude said:
This is what I have in the source code:
---8<---8<---8<---8<---
h = 0.105069988414
xx = range(-kappa, kappa+1, h)
---8<---8<---8<---8<---

This is what Python says: ValueError: range() step argument must not
be zero

Can step not be a float value?

Indeed. Older Pythons will warn you and then try to convert the arguments to
integers
__main__:1: DeprecationWarning: integer argument expected, got float
[0]

and in 2.7 or 3.x you'll get a type error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: range() integer end argument expected, got float.

Try numpy.arange() instead:
array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
 
M

MATLABdude

Try numpy.arange() instead:array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0..8,  0.9])

Thanks! It worked.

What's wrong with the following code?
---8<---8<---8<---
T0_orig = [5, 50, 500, 5000]
for counter in T0_orig:
T0 = (L**2)/(D*pi**2)*counter
amax = T0/kappa
alpha = (10**-6)*amax
lambda_, xx, f = nonhomog_solver(kappa, alpha, nu, nx)
V0 = sqrt( T0_orig*(m**-1) + pi**2 * D/(m*L**2)*lambda_ )
print "V0 = ", V0
print ""
---8<---8<---8<---

Python says:
---8<---8<---8<---
Traceback (most recent call last):
File "nonhomog.py", line 159, in <module>
main()
File "nonhomog.py", line 157, in main
nonhomog(0.2)
File "nonhomog.py", line 152, in nonhomog
V0 = sqrt( T0_orig*(m**-1) + pi**2 * D/(m*L**2)*lambda_ )
TypeError: can't multiply sequence by non-int of type 'float'
---8<---8<---8<---
 
A

Arnaud Delobelle

MATLABdude said:
Try numpy.arange() instead:
numpy.arange(0, 1, .1)
array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9])

Thanks! It worked.

What's wrong with the following code?
---8<---8<---8<---
T0_orig = [5, 50, 500, 5000]
for counter in T0_orig:
T0 = (L**2)/(D*pi**2)*counter
amax = T0/kappa
alpha = (10**-6)*amax
lambda_, xx, f = nonhomog_solver(kappa, alpha, nu, nx)
V0 = sqrt( T0_orig*(m**-1) + pi**2 * D/(m*L**2)*lambda_ )
print "V0 = ", V0
print ""
---8<---8<---8<---

Python says:
---8<---8<---8<---
Traceback (most recent call last):
File "nonhomog.py", line 159, in <module>
main()
File "nonhomog.py", line 157, in main
nonhomog(0.2)
File "nonhomog.py", line 152, in nonhomog
V0 = sqrt( T0_orig*(m**-1) + pi**2 * D/(m*L**2)*lambda_ )
TypeError: can't multiply sequence by non-int of type 'float'
---8<---8<---8<---

T0_orig is a list and you are trying to multiply this list by a float
(m**-1)
 
M

MATLABdude

T0_orig is a list and you are trying to multiply this list by a float
(m**-1)

Yes, yes of course. Thanks! :)

This works:
---8<---8<---8<---
T0_orig = [5, 50, 500, 5000]
for counter in T0_orig:
T0 = (L**2)/(D*pi**2)*counter
amax = T0/kappa
alpha = (10**-6)*amax
lambda_, xx, f = nonhomog_solver(kappa, alpha, nu, nx)
V0 = sqrt( counter*(m**-1) + pi**2 * D/(m*L**2)*lambda_ )
print "V0 = ", V0
print ""
---8<---8<---8<---
 
V

Victor Eijkhout

MATLABdude said:
Also the
values themselves are not identical compared to the values of the
MATLAB program.

In numerical analysis there is no such thing as identical. If they
differ by 1.e-10 I'd call it close enough. The difference comes from
differing numerical methods.

Victor.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top