error in solving acoupled,first order differential equations in ipython - scipy
I have a following coupled, first order differential equations:
dB(t)/dt=-a*A(t);
dA(t)/dt=b*B(t);
dJ(t)/dt=cA(t)-dB(t)
Solve the system using paramenters a = 0.05, b = 0.02, c = 0.03 and d = 0.04 of your own choice over a time interval from 0 to 200.
Here is my code:
a=0.05
b=0.02
c=0.03
d=0.04
def function(x,t):
x1, x2, x3 = x[0], x[1], x[2]
#x1, x2, x3 = A, B, J
dx1=b*x1
dx2=-a*x0
dx3=c*x0-d*x1
return [dx1, dx2, dx3]
x0 = [100,100, 1]
t = linspace(0, 200, 200)
x = odeint(function, x0, t)
The output I got is:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
8 #x1, x2, x3 = A, B, J
9 dx1=b*x1
---> 10 dx2=-a*x0
11 dx3=c*x0-d*x1
12 return [dx1, dx2, dx3]
TypeError: can't multiply sequence by non-int of type 'float'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
8 #x1, x2, x3 = A, B, J
9 dx1=b*x1
---> 10 dx2=-a*x0
11 dx3=c*x0-d*x1
12 return [dx1, dx2, dx3]
TypeError: can't multiply sequence by non-int of type 'float'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
8 #x1, x2, x3 = A, B, J
9 dx1=b*x1
---> 10 dx2=-a*x0
11 dx3=c*x0-d*x1
12 return [dx1, dx2, dx3]
TypeError: can't multiply sequence by non-int of type 'float'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
8 #x1, x2, x3 = A, B, J
9 dx1=b*x1
---> 10 dx2=-a*x0
11 dx3=c*x0-d*x1
12 return [dx1, dx2, dx3]
TypeError: can't multiply sequence by non-int of type 'float'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
8 #x1, x2, x3 = A, B, J
9 dx1=b*x1
---> 10 dx2=-a*x0
11 dx3=c*x0-d*x1
12 return [dx1, dx2, dx3]
TypeError: can't multiply sequence by non-int of type 'float'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
8 #x1, x2, x3 = A, B, J
9 dx1=b*x1
---> 10 dx2=-a*x0
11 dx3=c*x0-d*x1
12 return [dx1, dx2, dx3]
TypeError: can't multiply sequence by non-int of type 'float'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
8 #x1, x2, x3 = A, B, J
9 dx1=b*x1
---> 10 dx2=-a*x0
11 dx3=c*x0-d*x1
12 return [dx1, dx2, dx3]
TypeError: can't multiply sequence by non-int of type 'float'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
8 #x1, x2, x3 = A, B, J
9 dx1=b*x1
---> 10 dx2=-a*x0
11 dx3=c*x0-d*x1
12 return [dx1, dx2, dx3]
TypeError: can't multiply sequence by non-int of type 'float'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
8 #x1, x2, x3 = A, B, J
9 dx1=b*x1
---> 10 dx2=-a*x0
11 dx3=c*x0-d*x1
12 return [dx1, dx2, dx3]
TypeError: can't multiply sequence by non-int of type 'float'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
8 #x1, x2, x3 = A, B, J
9 dx1=b*x1
---> 10 dx2=-a*x0
11 dx3=c*x0-d*x1
12 return [dx1, dx2, dx3]
TypeError: can't multiply sequence by non-int of type 'float'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
8 #x1, x2, x3 = A, B, J
9 dx1=b*x1
---> 10 dx2=-a*x0
11 dx3=c*x0-d*x1
12 return [dx1, dx2, dx3]
TypeError: can't multiply sequence by non-int of type 'float'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
8 #x1, x2, x3 = A, B, J
9 dx1=b*x1
---> 10 dx2=-a*x0
11 dx3=c*x0-d*x1
12 return [dx1, dx2, dx3]
TypeError: can't multiply sequence by non-int of type 'float'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
8 #x1, x2, x3 = A, B, J
9 dx1=b*x1
---> 10 dx2=-a*x0
11 dx3=c*x0-d*x1
12 return [dx1, dx2, dx3]
TypeError: can't multiply sequence by non-int of type 'float'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
8 #x1, x2, x3 = A, B, J
9 dx1=b*x1
---> 10 dx2=-a*x0
11 dx3=c*x0-d*x1
12 return [dx1, dx2, dx3]
TypeError: can't multiply sequence by non-int of type 'float'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
8 #x1, x2, x3 = A, B, J
9 dx1=b*x1
---> 10 dx2=-a*x0
11 dx3=c*x0-d*x1
12 return [dx1, dx2, dx3]
TypeError: can't multiply sequence by non-int of type 'float'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
8 #x1, x2, x3 = A, B, J
9 dx1=b*x1
---> 10 dx2=-a*x0
11 dx3=c*x0-d*x1
12 return [dx1, dx2, dx3]
TypeError: can't multiply sequence by non-int of type 'float'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
8 #x1, x2, x3 = A, B, J
9 dx1=b*x1
---> 10 dx2=-a*x0
11 dx3=c*x0-d*x1
12 return [dx1, dx2, dx3]
TypeError: can't multiply sequence by non-int of type 'float'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
8 #x1, x2, x3 = A, B, J
9 dx1=b*x1
---> 10 dx2=-a*x0
11 dx3=c*x0-d*x1
12 return [dx1, dx2, dx3]
TypeError: can't multiply sequence by non-int of type 'float'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
8 #x1, x2, x3 = A, B, J
9 dx1=b*x1
---> 10 dx2=-a*x0
11 dx3=c*x0-d*x1
12 return [dx1, dx2, dx3]
TypeError: can't multiply sequence by non-int of type 'float'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
8 #x1, x2, x3 = A, B, J
9 dx1=b*x1
---> 10 dx2=-a*x0
11 dx3=c*x0-d*x1
12 return [dx1, dx2, dx3]
TypeError: can't multiply sequence by non-int of type 'float'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
8 #x1, x2, x3 = A, B, J
9 dx1=b*x1
---> 10 dx2=-a*x0
11 dx3=c*x0-d*x1
12 return [dx1, dx2, dx3]
TypeError: can't multiply sequence by non-int of type 'float'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
8 #x1, x2, x3 = A, B, J
9 dx1=b*x1
---> 10 dx2=-a*x0
11 dx3=c*x0-d*x1
12 return [dx1, dx2, dx3]
TypeError: can't multiply sequence by non-int of type 'float'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
8 #x1, x2, x3 = A, B, J
9 dx1=b*x1
---> 10 dx2=-a*x0
11 dx3=c*x0-d*x1
12 return [dx1, dx2, dx3]
TypeError: can't multiply sequence by non-int of type 'float'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
8 #x1, x2, x3 = A, B, J
9 dx1=b*x1
---> 10 dx2=-a*x0
11 dx3=c*x0-d*x1
12 return [dx1, dx2, dx3]
TypeError: can't multiply sequence by non-int of type 'float'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
8 #x1, x2, x3 = A, B, J
9 dx1=b*x1
---> 10 dx2=-a*x0
11 dx3=c*x0-d*x1
12 return [dx1, dx2, dx3]
TypeError: can't multiply sequence by non-int of type 'float'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
8 #x1, x2, x3 = A, B, J
9 dx1=b*x1
---> 10 dx2=-a*x0
11 dx3=c*x0-d*x1
12 return [dx1, dx2, dx3]
TypeError: can't multiply sequence by non-int of type 'float'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
8 #x1, x2, x3 = A, B, J
9 dx1=b*x1
---> 10 dx2=-a*x0
11 dx3=c*x0-d*x1
12 return [dx1, dx2, dx3]
TypeError: can't multiply sequence by non-int of type 'float'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
8 #x1, x2, x3 = A, B, J
9 dx1=b*x1
---> 10 dx2=-a*x0
11 dx3=c*x0-d*x1
12 return [dx1, dx2, dx3]
TypeError: can't multiply sequence by non-int of type 'float'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
8 #x1, x2, x3 = A, B, J
9 dx1=b*x1
---> 10 dx2=-a*x0
11 dx3=c*x0-d*x1
12 return [dx1, dx2, dx3]
TypeError: can't multiply sequence by non-int of type 'float'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
8 #x1, x2, x3 = A, B, J
9 dx1=b*x1
---> 10 dx2=-a*x0
11 dx3=c*x0-d*x1
12 return [dx1, dx2, dx3]
TypeError: can't multiply sequence by non-int of type 'float'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
8 #x1, x2, x3 = A, B, J
9 dx1=b*x1
---> 10 dx2=-a*x0
11 dx3=c*x0-d*x1
12 return [dx1, dx2, dx3]
TypeError: can't multiply sequence by non-int of type 'float'
Repeated convergence failures (perhaps bad Jacobian or tolerances).
Run with full_output = 1 to get quantitative information.
what is the problem of my code?
You have three first-order equations, not second order. (Or did you mean to write second order derivatives in your equations 1, 2, and 3?)
deriv should return the three values dA/dt, dB/dt and dJ/dt, but currently it returns an array of length 5.
zinit should contain three values: A(0), B(0) and J(0).
I think i have fixed it.
a=0.05
b=0.02
c=0.03
d=0.04
def function(x,t):
x1, x2, x3 = x[0], x[1], x[2]
#x1, x2, x3 = A, B, J
dx1=b*x2
dx2=-a*x1
dx3=c*x1-d*x2
return [dx1, dx2, dx3]
x0 = [100,100, 1]
t = linspace(0, 200, 200)
x = odeint(function, x0, t)
Related
Invalid name argument in Octave ztest
I am trying to make a Z-test in Octave, but I have a weird error. This is my code: A =[ 7 7 4 5 9 9 4 12 8 1 8 7 3 13 2 1 17 7 12 5 6 2 1 13 14 10 2 4 9 11 3 5 12 6 10 7 ]; x = A(:)'; n = length(x); fprintf(' Volume %d \n',n) med = mean(x); fprintf(' Sample mean %f\n',med) sigma = 5; m0 = 9; type = -1; %left tailed test alpha1 = 0.05; %significance level alpha2 = 0.01; fprintf('\n') fprintf('SIGNIFICANCE LEVEL %f:\n',alpha1) [h1, p1, ci1, zstat1] = ztest(x, m0, sigma, alpha1, type); And the error is: error: Invalid Name argument. error: called from ztest at line 70 column 9 Pb1a at line 26 column 23 I'd like to know why this happens. Hope it is not because I am using Octave instead of Matlab. Actually that is...it works in MATLAB Online.
The error is pretty explanatory: the syntax you are using (five numeric arguments) is not supported by Octave. In fact, in Matlab that syntax is not documented either, and seems to be supported only for backward compatibility, as can be seen in ztest source code: if nargin>=4 <p> if isnumeric(varargin{1}) % Old syntax % ZTEST(X,M,SIGMA,ALPHA,TAIL,DIM) [...] The solution is to use the syntax officially supported by both Matlab and Octave, with arguments beyond the third specified as parameter, value pairs: [h1, p1, ci1, zstat1] = ztest(x, m0, sigma, 'alpha', alpha1, 'tail', 'left');
Constraints in scipy.optimize throwing x0 error
Looking to take a list of stocks and adjust their weight in a portfolio until the overall portfolio beta is 1.0 the output of "stonkBetas" is static and is: [3.19292010501853, 0.7472001935364129, 1.0889157697158605, 0.8944059912707691, 0.04192080860817828, 1.0011520737327186, 0.9155119223385676] I then create two functions. One to define how the betas are weighted. The second just as con that will make the constraint that the sum of the minimized weighted portfolio will have an overall summed beta of 1.0. def betaOpp(weights): a,b,c,d,e,f,g=weights f=a*stonkBetas[0]+b*stonkBetas[1]+c*stonkBetas[2]+d*stonkBetas[3]+e*stonkBetas[4]+f*stonkBetas[5]+g*stonkBetas[6] return f initial_guess = [.1,.1,.1,.1,.2,.2,.2] print('hi') print(sum(initial_guess)) print('bye') def con(t): print('this should be zero:') print(sum(t)-1) return sum(t) - 1.0 cons = {'type':'eq', 'fun': con} bnds = ((.02,.8),(.02,.8),(.02,.8),(.02,.8),(.02,.8),(.02,.8),(.02,.8)) res = optimize.minimize(betaOpp,initial_guess, bounds=bnds, constraints=cons) print(res) This gives me this output hi 1.0 bye this should be zero: 0.0 this should be zero: 0.0 this should be zero: 0.0 this should be zero: 1.4901161193847656e-08 this should be zero: 1.4901161193847656e-08 this should be zero: 1.4901161193847656e-08 this should be zero: 1.4901161193847656e-08 this should be zero: 1.4901161193847656e-08 this should be zero: 1.4901161193847656e-08 this should be zero: 1.4901161193847656e-08 this should be zero: 6.661338147750939e-16 --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-30-6567109e94a4> in <module> 16 cons = {'type':'eq', 'fun': con} 17 bnds = ((.02,.8),(.02,.8),(.02,.8),(.02,.8),(.02,.8),(.02,.8),(.02,.8)) ---> 18 res = optimize.minimize(betaOpp,x0=initial_guess, bounds=bnds, constraints=cons) 19 print(res) /opt/miniconda3/lib/python3.6/site-packages/scipy/optimize/_minimize.py in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options) 624 elif meth == 'slsqp': 625 return _minimize_slsqp(fun, x0, args, jac, bounds, --> 626 constraints, callback=callback, **options) 627 elif meth == 'trust-constr': 628 return _minimize_trustregion_constr(fun, x0, args, jac, hess, hessp, /opt/miniconda3/lib/python3.6/site-packages/scipy/optimize/slsqp.py in _minimize_slsqp(func, x0, args, jac, bounds, constraints, maxiter, ftol, iprint, disp, eps, callback, finite_diff_rel_step, **unknown_options) 424 425 if mode == -1: # gradient evaluation required --> 426 g = append(sf.grad(x), 0.0) 427 a = _eval_con_normals(x, cons, la, n, m, meq, mieq) 428 /opt/miniconda3/lib/python3.6/site-packages/scipy/optimize/_differentiable_functions.py in grad(self, x) 186 if not np.array_equal(x, self.x): 187 self._update_x_impl(x) --> 188 self._update_grad() 189 return self.g 190 /opt/miniconda3/lib/python3.6/site-packages/scipy/optimize/_differentiable_functions.py in _update_grad(self) 169 def _update_grad(self): 170 if not self.g_updated: --> 171 self._update_grad_impl() 172 self.g_updated = True 173 /opt/miniconda3/lib/python3.6/site-packages/scipy/optimize/_differentiable_functions.py in update_grad() 90 self.ngev += 1 91 self.g = approx_derivative(fun_wrapped, self.x, f0=self.f, ---> 92 **finite_diff_options) 93 94 self._update_grad_impl = update_grad /opt/miniconda3/lib/python3.6/site-packages/scipy/optimize/_numdiff.py in approx_derivative(fun, x0, method, rel_step, abs_step, f0, bounds, sparsity, as_linear_operator, args, kwargs) 389 390 if np.any((x0 < lb) | (x0 > ub)): --> 391 raise ValueError("`x0` violates bound constraints.") 392 393 if as_linear_operator: ValueError: `x0` violates bound constraints. And I just don't understand where I'm going wrong. The x0 is perfectly 1.0 - I can see it! Hopefully I'm just doing something stupid here. Please help!
Noncomformant argument in Octave
Why am I getting this error? Where is problem? >> x=1:1:10 x = 1 2 3 4 5 6 7 8 9 10 >> y=(2*pi)/(x) error: operator /: nonconformant arguments (op1 is 1x1, op2 is 1x10)
How to apply a function into every element
If I have vector [1,2,3,4],I hope to use a function f into every element to get [f(1),f(2),f(3),f(4)] If I have a matrix mat >> mat=magic(3) mat = 8 1 6 3 5 7 4 9 2 I hope to get f(8) f(1) f(6) f(3) f(5) f(7) f(4) f(9) f(2) Is any simple method to do this in matlab?
Solution Use MATLAB's arrayfun function as follows: arrayfun(f,mat) Example mat = magic(3); %defines input f = #(x) x.^2; %defines f (f is the square function) arrayfun(f,mat); %applies f on mat Results mat = 8 1 6 3 5 7 4 9 2 arrayfun(f,mat)= 64 1 36 9 25 49 16 81 4
MatLab accumarray unexpectedly changing ordering
As long as I understood accumarray, it means "Making the nth row of the output: 1) find n in sub. 2) if n is in m1, m2, m3 th element in sub, 3) apply the function to m1,m2,m3 th element of val 4) that's the nth row of the output" Am I wrong somewhere? I ran the following code. A = [2 10 13 ; 1 11 14; 1 12 10] [U,ix,iu]= unique(A(:,1)) vals = reshape(A(:, 2:end).', [], 1) subs = reshape(iu(:, ones(size(A, 2)-1,1)).', [], 1) r2 = accumarray(subs, vals', [], #(x){x'}) r2{1} r2{2} A = 2 10 13 1 11 14 1 12 10 U = 1 2 ix = 3 1 iu = 2 1 1 vals = 10 13 11 14 12 10 subs = 2 2 1 1 1 1 r2 = [1x4 double] [1x2 double] ans = 12 11 14 10 ans = 13 10 ========================= But I expected r{1} = 11 14 12 10, and r{2} = 10 13. Why did accumarray suddenly changed the ordering? How can I get the expected result?
The documentation of accumarray says: Note If the subscripts in subs are not sorted, fun should not depend on the order of the values in its input data. And your subs is not sorted (at least not in ascending order). If you rewrite the code so that subs is sorted and vals is also rearranged accordingly you get the desired result: A = [2 10 13 ; 1 11 14; 1 12 10] [U,ix,iu]= unique(A(:,1)) vals = reshape(A(:, 2:end).', [], 1) subs = reshape(iu(:, ones(size(A, 2)-1,1)).', [], 1) [subs_sorted, I] = sort(subs); r2 = accumarray(subs_sorted, vals(I)', [], #(x){x'}) r2{1} r2{2} And running this code returns: ans = 11 14 12 10 ans = 10 13