I tried searching in the forum and I was not successful in finding the reason why the answers in python are different than in Matlab. I am trying to use sind() function in Matlab, where the user input is in degrees. The Matlab snippet is,
angle = 27;
b = sind(angle)
This gives b as 0.4540.
The equivalent code in python
angle = 27;
b = math.degrees(math.sin(angle))
I get b as 54.79.
I can't able to fix the problem and any inputs would be highly appreciable.
Best Regards
Pradeep
This is a unit issue. In python, math.sin() assumes radians, not degrees. The MATLAB function sind specifies degrees. So you need to convert your angle into radians, then take the sine.
Here's the python you need:
math.sin(math.radians(27))
Related
I've written a large program, with dependencies on libraries written in my lab. I'm getting wrong (and somewhat random) results, which are caused by floating-point errors.
I would like to do some python magic and change all floats to decimals, or some other more precise type.
I can't write the full code here, but following is the general flow -
def run(n):
...
x = 0.5 # initializes as float
for _ in range(n):
x = calc(x)
...
return x
What I'm trying to avoid is to go over all initialization in the code and add a manual cast to decimal.
Is there a trick I can do to make python initialize all floats in lines such as x = 0.5 as decimals? or perhaps use a custom interpreter which has more exact floats?
Thanks,
I can't post the full code, hope my edit makes it clearer.
I think you can use this:
from decimal import Decimal
Decimal(variable)
Are there anyone help me how to write this polynomial into python language please, I’ve tried my best, but it’s too hard
P/s sorry for my bad grammar, i’m from vietnam
Assuming you simply want to be able to get y result for either one of those equations, you can just do the following:
import math
def y1(x):
return 4*(x*x + 10*x*math.sqrt(x)+3*x+1)
def y2(x):
return (math.sin(math.pi*x*x)+math.sqrt(x*x+1))/(exp(2*x)+math.cos(math.pi/4*x))
If you want to evaluate y1 or y2 given a certain x, just use y1(x), for example:
print(y1(10))
print(y2(10))
If you want to be able to plot those equations in python, try using the python turtle module to do so.
I have the datafile:
10.0000 -330.12684910
15.0000 -332.85109334
20.0000 -333.85785274
25.0000 -334.18315783
30.0000 -334.28078907
35.0000 -334.30486903
40.0000 -334.30824069
45.0000 -334.30847874
50.0000 -334.30940105
55.0000 -334.31091085
60.0000 -334.31217217
The commands a used to fit this
f(x) = a+b*exp(c*x)
fit f(x) datafile via a, b, c
didn't get the negative exponential that I expected, then just to see how the hyperbola fitted I tried
f(x) = a+b/x
fit f(x) datafile via a, b
but decided to do this:
f(x) = a+b*exp(-c*x)
fit f(x) datafile via a, b, c
and it worked. I continued doing fits but in some point it started to mark this error undefined value during function evaluation.
I restarted the session and deleted the fit.log file, I thought it was a gnuplot bug but since then I always receive the undefined value error. I've been reading similar issues. It could be a, b, c seeds but I have introduced very similar values to the ones a received that time it fitted well but didn't work. I'm thinking the problem might be chaotic or I'm doing something wrong.
Thank you for your method. I understand gnuplot uses non linear least squares method for fitting.
I found one solution is to use the model y=a+b*exp(-c*c*x), I also found better initial values and it worked. anyway I have this other dataset:
2 -878.11598213
6 -878.08846509
10 -878.08105262
19 -878.07882425
28 -878.07793702
44 -878.07755010
60 -878.07738151
85 -878.07729504
110 -878.07725107
And gnuplot fit does the work but really bad. instead I used your method, here I show a comparison:
gnuplot fit and jjaquelin comparison
it is way better.
I don't know the algorithm used by gnuplot. Probably an iterative method starting from guessed values of the parameters. The difficulty might come from not convenient initial values and/or from no convergence of the process.
From my own calculus the result is close to the values below. The method, which is not iterative and doesn't require initial guess, is explained in the paper : https://fr.scribd.com/doc/14674814/Regressions-et-equations-integrales
FOR INFORMATION :
The linearisation of the regression is obtained thanks to the integral equation
to which the function to be fitted is solution.
The paper referenced above is mainly written in French. It is pratially translated in : https://scikit-guess.readthedocs.io/en/latest/appendices/references.html
I'm reprogramming an orbital analysis program that I wrote in MATlab in Python 3.7. The initial inputs of velocity and position are queried user inputs. The method I'm using currently is clunky feeling (I am a python beginner) and I'm wondering if someone can suggest a more elegant method to take this input vector as a numpy float64? I suspect this problem is trivial but I haven't found a clear answer yet...
The current input is a vector with the syntax: "i,k,j". no spaces, comma delimited. Each component is converted to a float64 in a list via list(map(float, input)), I then have to convert it back to numpy float64 in order to use r as a vector later on.
v = np.float64(list(map(np.float64,input('Query Text').split(','))))
I'd say that's pretty elegant already. I'd do it like this if you like it better:
np.float64(
[np.float64(i) for i in input("Query text").split(",")]
)
but i wouldn't say this is much more elegant, but at least it does the same thing.
I am trying to simulate a number of different distribution types for a project using Excel. Right now, I have generated a normal distribution with a mean of 35 and a standard deviation of 3.33. So far so good.
I would like to also generate some other distribution types.
One I have tried is a lognormal. To get that, I am using the following code:
=(LOGNORM.INV(RAND(),LN(45^2/SQRT(45^2+3.33^2)),SQRT(LN((45^2+3.33^2)/4.5^2))
It produces some output, but I would welcome anyone's input on the syntax.
What I really want to try to do is a power law distribution. From what I can tell, Excel does not have a built-in function to randomly generate this data. Does anyone know of a way to do it, besides switching software packages?
Thanks for any help you can provide.
E
For the (type I) Pareto distribution, if the parameters are a min value xm and an exponent alpha then the cdf is given by
p = 1 - (xm/x)^alpha
This gives the probability, p, that the random variable takes on a value which is <= x. This is easy to invert, so you can use inverse sampling to generate random variables which follow that distribution:
x = xm/(1-p)^(1/alpha) = xm*(1-p)^(-1/alpha)
If p is uniform over [0,1] then so is 1-p, so in the above you can just use RAND() to simulate 1/p. Thus, in Excel if you wanted to e.g. simulate a type-1 Pareto distribution with xm = 2 and alpha = 3, you would use the formula:
= 2 * RAND()^(-1/3)
If you are going to be doing this sort of thing a lot with different distributions, you might want to consider using R, which can be called directly from Excel using the REXcel add-in. R has a very large number of built-in distributions that it can directly sample from (and it also uses a better underlying random number generator than Excel does).