1

I am trying to convert this Matlab code into python, but due to no experience with Matlab I just can't figure out the way I can do it. Could someone help? Basically, only the fplot command makes me confusing.

E = 95;
m = 250;
c = 0.900;
t = 0:1:100;
T = E*t/(m*c);
f = @(t)E*t/(m*c);
fplot(f,[0,100]);
3
  • Hi Nikita, welcome to SO! Basically, fplot(fun,[xmin,xmax]) plots the function fun between the x-axis limits xmin and xmax. Also notice that f(t)==T(t+1) (I'm not sure why whoever wrote this program defined both T and f; maybe to demonstrate different programming strategies?) Commented Mar 20, 2021 at 20:27
  • @Vicky check out my code, if any issue please mention for future Commented Mar 20, 2021 at 20:49
  • @ExplooreX I don't know much about Python, but I guess t has been offset by 1 in your code, has it not? Also, the code seems to translate a use of what would be the plot() function in MATLAB, not fplot(), right? Commented Mar 20, 2021 at 21:01

2 Answers 2

3

In my opinion, the answer of Exploore X is not so good (you can lose the calculation speed by about 100 times). Try to use numpy for the best MATLAB code translation (because numpy also uses the concept of vectorization).

Thus, you can use something like this, which looks more like your MATLAB code:

import matplotlib.pyplot as plt
import numpy as np

E = 95
m = 250
c = 0.900
t = np.arange(100+1)
T = E*t/(m*c)
# or using anonymous function ( @(t) ... in matlab )
f = lambda t: E*t/(m*c)

plt.plot(t, T)
# or
# plt.plot(t, f(t))
plt.show()

Using a search engine, you can find many articles about Matlab and Python compatibility (in the sense of the concept of vectorization, for example http://mathesaurus.sourceforge.net/matlab-numpy.html).

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

5 Comments

Thanks for that but I want questioner get the Idea, but If I will use Numpy whole purpose will get destroy.
Yaa we know that working with vectorization is much easier than list comprehension
You don't need the f definition line. T is directly defined as an array of E*(each point) / (m*c)
@Mathieu, I have used/translated the matlab code from the question
Ok sure, but Matlab and python are too different languages anyway. Let's make this into something which makes sense in Python. This line defining the function is useless in python as the array of function points is already created anyway. + it's confusing as the variable t used in the line defining f is not the same as the variable t in which the np.arange is stored.
1

First their are few things to note here :

f = @(t)E*t/(m*c);
fplot(f,[0,100]);

@(t) E*t/(m*c) here @(t) is a variable in equation E*t/(m*c) remaining is constants.

fplot(f, [0, 100]) is function plot where t varies from 0 to 100

PYTHON LOGIC :

import matplotlib.pyplot as plt
E = 95
m = 250
c = 0.900
t = list(range(1, 101))
T = [ E*i/(m*c) for i in t]

plt.plot(T)
plt.show()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.