I've been trying to run this on Scilab, but I can't get it to work.
global ke Yxs Va Kg A D cx cxa cxamax fbc Kp Yps Ypx umax rho V mg mga mbc nxa
ke=0.003/(10^9);
Yxs=45*10^9;
Va=0.005;
Kg=0.04;
A=0.004657;
D=4.5*10^(-10);
cx=10^9;
cxa=10^9;
cxamax=1163*10^9;
fbc=0.01;
Kp=0.01;
Yps=0.9;
Ypx=0.01125;
umax=1.6;
rho=1030;
V=0.2;
function ydot=g(t,y)
ydot=zeros(y);
ydot(1)=((umax*((y(3)/Va)/((y(3)/Va)+Kg)))-ke*Yxs)*y(1);
ydot(2)=-(D*A*(((y(2)/V)-(y(3)/Va))/((y(4)/(rho*fbc))/A)));
ydot(3)=(D*A*(((y(2)/V)-(y(3)/Va))/((y(4)/(rho*fbc))/A)))-((umax*((y(3)/Va)/((y(3)/Va)+Kg)))/Yxs+(Ypx*(umax*((y(3)/Va)/((y(3)/Va)+Kg)))*(y(3)/Va)/(cga+Kp))/Yps)*y(1);
ydot(4)=(Ypx*(umax*((y(3)/Va)/((y(3)/Va)+Kg)))*(y(3)/Va)/(cga+Kp))*y(1);
endfunction
t0=0;
t=0:1:50;
y0=ones(0,4,4,0);
y=ode(y0,t0,t,g);
This is what the console returns when I run it.
lsoda-- neq (=i1) .lt. 1
where i1 is : 0
Illegal input detected (see printed message).
at line 52 of executed file C:\Users\pandrade\Documents\Pessoal\Ufla\modelagem artigo.sce
ode: lsoda exit with state -3.
Line 52 is the last line of the code, where the ode command is. Does anyone know what's wrong with it?
Your intialization of y0 is incorrect. Maybe you meant:
y0=[0;4;4;0]
or
y0=ones(4,1)
In any case y0 should be a column vector of 4 elements. Moreover you probably misspelled cga instead of cxa in the code of g(t,y).
Remember to always test that your right hand side function (here g) works outside the context of the ode call, by typing something like
g(0,y0)
on the Scilab command line.
Related
I learnt how to differentiate on octave through this. But I now want to plot graph of the same function on octave. I am not able to do so. I want to know what the right command for plotting the 'ffd' equation in the code is.
`f = #(x) x.^2 + 3*x - 1 + 5*x.*sin(x);
pkg load symbolic
syms x;
ff = f(x);
ffd = diff(ff, x) `
What I have tried so far: I have tried adding these lines of code to the end and it didn't work. The graph was empty and I got an error.
real
ffd = (sym) 5⋅x⋅cos(x) + 2⋅x + 5⋅sin(x) + 3
error: __go_line__: invalid value for array property "ydata", unable to create graphics handle
error: called from
__plt__\>__plt2vs__ at line 466 column 15
__plt__\>__plt2__ at line 245 column 14
__plt__ at line 112 column 18
plot at line 229 column 10
real at line 10 column 1\`
I was expecting it to plot the graph of (sym) 5⋅x⋅cos(x) + 2⋅x + 5⋅sin(x) + 3 i.e., ffd, but it didn't work
In order to draw graphs of the differentiated functions we need to have code similar to this and in this case I am using pkg symbolic
pkg load symbolic
syms x;
x = sym('x');
y = #(x) x.^3;
yy = y(x);
ffd = diff(diff(yy,x));
ffe = diff(yy,x);
ez1=ezplot(y,[-10,10])
hold on
ez2=ezplot(ffe,[-10,10])
hold on
ez3=ezplot(ffd,[-10,10])
note: hold on function is used to draw more than one graphs on the same screen. However, if you modify the program and run it again it doesn't clear the screen of the previous graphs, if anyone knows how to do this, please leave a comment.
I have to fit a dataset to the function f(x) = B + f(0)*exp(-x/t)
I get an error saying 'undefined value during function evaluation'. I presume the error is in the following bit of code:
f0 = 9444
f(x)= b + f0*exp(-x/t)
fit f(x) 'dataset.txt' using 2 : 3 via b,t
Where have I gone wrong?
Probably the search for values of t hit t=0 and evaluation failed on a divide-by-zero error.
Try setting f(x) = b + f0 * exp(-x*t) instead. You can invert t after finding a solution.
However, if you are describing exponential decay as a function of time t then you have your variable names confusingly reversed. The more conventional way of writing this would be
f(t) = b + f0 * exp(-x*t)
I am trying to use SymPy to solve for the variable p by defining the function as shown.
On execution, an error " can't convert expression to float" is being raised. I have just started learning Python so I will really appreciate if someone can help me understand what's causing the error and suggest a way to fix it and solve for p.
Note- For the assigned vales of G, F and h, p= 91.65
Thanks!
G=0.05
F=0.32
h=231 # these values should yield p = 91.65
def get_p (G,F,h):
p = symbols('p')
C2 = 3.14 * h * F*F
C1= 3.14 * G *h
eq_p=Eq(p *(1/math.cosh(p/C1))**2 - C1* (math.tanh(p/C1))+C2==0) #raises the error "cant convert expression to float"
sol_p=solve(eq_p)
return sol_p
p= get_p (G, F, h)
print(p)
When you get an error like this and have no idea why it is happening use an interactive session to work with smaller and smaller parts of the offending line until the problem is located. In your case the issue is that math.cosh and math.tanh expect numeric arguments and you are giving it something with a SymPy symbol p. Fortunately, Sympy has its own hyperbolic functions. So, remove the 'math.' and then at the top use:
>>> from sympy import cosh, tanh
But another problem will become apparent: the Eq argument looks like 'expr==0' and it should be 'expr, 0' since args of Eq are the L and R in L == R: it should be in the form of Eq(L, R).
Finally, check your expression with print(eq_p.subs(p, 91.65))...it doesn't appear to be close to zero (and a plot of the function confirms that it does not go through 0). So you have perhaps interpreted the original expression you are trying to solve incorrectly in writing it here.
Once you have the right equation then I would recommend using nsolve(eq_p, guess) where guess is an approximate value of the solution for p. You can import that with from sympy import nsolve.
Writing a python script to calc Implied Normal Vol ; in line with Jekel article (Industry Standard).
https://jaeckel.000webhostapp.com/ImpliedNormalVolatility.pdf
They say they are using a Generalized Incomplete Gamma Function Inverse.
For a call:
F(x)=v/(K - F) -> find x that makes this true
Where F is Inverse Incomplete Gamma Function
And x = (K - F)/(T*sqrt(T) ; v is the value of a call
for that x, IV is =(K-F)/x*sqrt(T)
Example I am working with:
F=40
X=38
T=100/365
v=5.25
Vol= 20%
Using the equations I should be able to backout Vol of 20%
Scipy has upper and lower Incomplete Gamma Function Inverse in their special functions.
Lower: scipy.special.gammaincinv(a, y) : {a must be positive param}
Upper: scipy.special.gammainccinv(a, y) : {a must be positive param}
Implementation:
SIG= sympy.symbols('SIG')
F=40
T=100/365
K=38
def Objective(sig):
SIG=sig
return(special.gammaincinv(.5,((F-K)**2)/(2*T*SIG**2))+special.gammainccinv(.5,((F-K)**2)/(2*T*SIG**2))+5.25/(K-F))
x=optimize.brentq(Objective, -20.00,20.00, args=(), xtol=1.48e-8, rtol=1.48e-8, maxiter=1000, full_output=True)
IV=(K-F)/x*T**.5
Print(IV)
I know I am wrong, but Where am I going wrong / how do I fix it and use what I read in the article ?
Did you also post this on the Quantitative Finance Stack Exchange? You may get a better response there.
This is not my field, but it looks like your main problem is that brentq requires the passed Objective function to return values with opposite signs when passed the -20 and 20 arguments. However, this will not end up happening because according to the scipy docs, gammaincinv and gammainccinv always return a value between 0 and infinity.
I'm not sure how to fix this, unfortunately. Did you try implementing the analytic solution (rather than iterative root finding) in the second part of the paper?
Can someone help me understand this "Rocky Mountain BASIC" or "HTBasic" code?
I have to find out why the print functionality doesn't work anymore.
First, this line
PRINTER IS 26
I understand that the printer that we are going to use is "26" but what does 26 mean?
REPEAT
IF LWC$(Imp$)="o" THEN
PRINTER IS 26
FOR I=0 TO VAL(Mesu$(0,5))
FOR L=0 TO 6
PRINT Mesu$(I,L)
NEXT L
NEXT I
ELSE
FOR L=0 TO 6
PRINT TABXY(2,9+L);Mesu$(0,L)
NEXT L
FOR C=1 TO VAL(Mesu$(0,5))
PRINT TABXY(20-36*(C>3)+(C-1)*12,8+8*(C>3)),"voie "&VAL$(C-1)
FOR L=1 TO 7
PRINT TABXY(20-36*(C>3)+(C-1)*12,L+8+8*(C>3)),Mesu$(C,L-1)
NEXT L
NEXT C
END IF
INPUT "SORTIE sur l'IMPRIMANTE O/N ?",Imp$
UNTIL LWC$(Imp$)="n"
“26” is one of the codes that specifies an output port for the PRINT statement. For example,
PRINTER IS CRT
PRINTER IS PRT
The letter codes correspond to number codes; PRINTER IS CRT is the same as PRINTER IS 1, for example, and PRT is the same as 701.
The codes that are likely to work for printing in this BASIC dialect, including 26, are:
26 701 9 15 19 23 24 25
I pulled this from an ancient document, Using HP BASIC For Instrument Control: A Self-Study Course, which you may find useful. (I suspect you meant HPBasic, not HTBasic, in your subject line?)
TABXY is a variant of the PRINT statement, for printing to specific locations on a CRT screen; the docs I’m seeing say that the XY is ignored if not printing to a CRT, but I wouldn’t be surprised if TABXY also worked on some plotters. The first two numbers would be the X and Y coordinates to begin displaying the text, with TABXY(1, 1) indicating the upper left corner, and the lower right corner depending on how many columns and rows the CRT has.
You may find the HP9000 series BASIC Language Reference, Volume 1 and BASIC Language Reference, Volume 2 useful.
LWC$ is just a lowercase function, to ensure that that whether the user inputs “O”, “N”, “o”, or “n” at the INPUT line, the program will respond correctly.
VAL converts a string to the number that that string represents. The string “3” would become the number 3, for example.
The variables Mesu$ is likely a two-dimensional array, with x from 0 to, judging from line 4, a variable amount contained in Mesu$(0, 5) and y from 0 to 6, judging from line 5.
I guess that the line with PLOTTER IS 26 and we say that we want colors.
MAT Menu$=("")
DISP "envoi à l' imprimante .."
Menu$(1)="PLOTTER"
Menu$(2)="IMPRIMANTE COULEUR"
!Select(0,1,Tp,26,12,1)
IF Tp=1 THEN
PLOTTER IS 705,"HPGL"
ELSE
PLOTTER IS 26,"HPGL;PCL5;COLOR,1600",0,260,0,185
END IF