0

Matlab code :

 Sol(indx,j) = mf* ((alpha/dx^2)*(Sol(indx+1,j-1)-2*Sol(indx,j-1)+Sol(indx-1,j-1))...
        + (K/dt)*Sol(indx,j-1) +(1/dt^2)*(2*Sol(indx,j-1) - Sol(indx,j-2)));

The code I translate :

 Sol[indx,j] = mf* ((alpha/(dx**2))*(Sol[indx+1,j-1]-2*Sol[indx,j-1]+Sol[indx-1,j-1])+ (K/dt)*Sol[indx,j-1] +(1/(dt**2))*(2*Sol[indx,j-1] - Sol[indx,j-2]))

I do not think I translate it correctly because the result does not match

Could someone help me? Thanks

edit: I really want to give some test value, but the matrix is about 200*200. I do not think copying/pastng those values is a good idea. And I cannot give you guys the full code because it is an homework question.

5
  • If you're using Python 2.x, be sure not to try testing with an integer value for dt, because then you'll run into integer division problems (e.g. 1 / 4 = 0). To avoid this, replace your 1/ with 1., or, better, just ensure dt is a float; e.g. dt = float(dt) Commented Apr 2, 2017 at 3:15
  • @ramcdougal dt is 0.001 in my code Commented Apr 2, 2017 at 3:43
  • 1
    You will get more and better answers if you create a Minimal, Complete, and Verifiable example. Especially make sure that the input and expected data are complete (not pseudo-data), and can be easily cut and and paste into an editor to allow testing proposed solutions. Commented Apr 2, 2017 at 4:21
  • 1
    You need to provide a test array on the order of a 10x10. The only way I could be sure that a numpy code matched a MATLAB one is to run it. Especially since both run. Anything else is a useless mental exercise. Commented Apr 2, 2017 at 8:58
  • Are j or indx ever 0? Because if they are they will do the completely wrong thing here. How are indx and j chosen? Are you using a loop or are they specified some other way? If a loop, how are the loop terms chosen? Commented Apr 3, 2017 at 2:08

3 Answers 3

1

It looks fine. Did you try to find what's the difference in the two results? Two issues I can think of are indexing and type casting: - MATLAB indexing starts with 1 whereas python starts with 0 - (alpha/(dx**2)) and (1/(dt**2)) : you could try making it (alpha./(dx**2)) and (1./(dt**2))

Sign up to request clarification or add additional context in comments.

3 Comments

In matlab "if length(find(Sol<0)) == 0 && length(find(Sol==Inf)) == 0 && Sol(20,10001) <1" will be true in some point. However, in Python, "((len(np.nonzero(Sol<0)) == 0) and (len(np.nonzero(Sol==np.inf)) == 0) and (Sol[19,10000] <1)):" will never be true
@Sakthi - saying that the code 'looks fine' doesn't answer the question. The FAQ explains how to leave good answers: stackoverflow.com/help/how-to-answer
I wouldn't be picky about this answer, given that the question lacks a proper test case. Answering with your own test code is more than what the question deserves at this time.
0

If you're using Python 2.7 (?) then division returns the floor value. You can return true division values by importing 'division':

e.g.

1/2
>>0

from __future__ import division
1/2
>>0.5

Python3+ does true division right out of the box though.

1 Comment

every value in my code in float besides j and indx matrix
0

I think the problem is in indexing, cause MATLAB indexing starts from 1 but Python's starts from 0. Try to decrease indx and j by 1 before running the expression. If j is an index in loop, just start loop from 0 not 1.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.