dsolve throwing an error while trying to solve system of ODEs - ode

syms X1(t) X2(t) X3(t) X4(t) X5(t) X6(t) X7(t) X8(t) X9(t) X10(t)
X = [X1; X2; X3; X4; X5; X6; X7; X8; X9; X10];
X0 = X_0;
eqn = diff(X) == -1*A*X;
[X1Sol(t) X2Sol(t) X3Sol(t) X4Sol(t) X5Sol(t) X6Sol(t) X7Sol(t) X8Sol(t) X9Sol(t) X10Sol(t) ] = dsolve(eqn, X0);
Where A is some 10x10 Matrix and X_0 is a column vector with 10 elements whose values are calculated beforehand. When I compile this code Matlab throws me the following error:
Error using mupadengine/feval (line 157) MuPAD error: Error: Invalid
equations. [ode::new]
Error in dsolve>mupadDsolve (line 325) T =
feval(symengine,'symobj::dsolve',sys,x,options);
Error in dsolve (line 186) sol = mupadDsolve(args, options);
Can anyone tell me what is going on and what is the rectification? I use Matlab R2012a.

I fail to find any error when include X_0 and A in sysms
syms X1(t) X2(t) X3(t) X4(t) X5(t) X6(t) X7(t) X8(t) X9(t) X10(t) X_0 A
X = [X1; X2; X3; X4; X5; X6; X7; X8; X9; X10];
X0 = X_0;
eqn = diff(X) == -1*A*X;
[X1Sol(t) X2Sol(t) X3Sol(t) X4Sol(t) X5Sol(t) X6Sol(t) X7Sol(t) X8Sol(t) X9Sol(t) X10Sol(t) ] = dsolve(eqn,X0)

Related

Fast summing of subarrays in Python

I have a data cube a of radius w and for every element of that cube, I would like to add the element and all surrounding values within a cube of radius r, where r < w. The result should be returned in an array of the same shape, b.
As a simple example, suppose:
a = numpy.ones(shape=(2*w,2*w,2*w),dtype='float32')
kernel = numpy.ones(shape=(2*r,2*r,2*r),dtype='float32')
b = convolve(a,kernel,mode='constant',cval=0)
then b would have the value (2r)(2r)(2r) for all the indices not on the edge.
Currently I am using a loop to do this and it is very slow, especially for larger w and r. I tried scipy convolution but got little speedup over the loop. I am now looking at numba's parallel computation feature but cannot figure out how to rewrite the code to work with numba. I have a Nvidia RTX card so CUDA GPU calculations are also possible.
Suggestions are welcome.
Here is my current code:
for x in range(0,w*2):
print(x)
for y in range(0,w*2):
for z in range(0,w*2):
if x >= r:
x1 = x - r
else:
x1 = 0
if x < w*2-r:
x2 = x + r
else:
x2 = w*2 - 1
if y >= r:
y1 = y - r
else:
y1 = 0
if y < w*2-r:
y2 = y + r
else:
y2 = w*2 - 1
if z >= r:
z1 = z - r
else:
z1 = 0
if z < w*2-r:
z2 = z + r
else:
z2 = w*2 - 1
b[x][y][z] = numpy.sum(a[x1:x2,y1:y2,z1:z2])
return b
Here is a very simple version of your code so that it works with numba. I was finding speed-ups of a factor of 10 relative to the pure numpy code. However, you should be able to get even greater speed-ups using a FFT convolution algorithm (e.g. scipy's fftconvolve). Can you share your attempt at getting convolution to work?
from numba import njit
#njit
def sum_cubes(a,b,w,r):
for x in range(0,w*2):
#print(x)
for y in range(0,w*2):
for z in range(0,w*2):
if x >= r:
x1 = x - r
else:
x1 = 0
if x < w*2-r:
x2 = x + r
else:
x2 = w*2 - 1
if y >= r:
y1 = y - r
else:
y1 = 0
if y < w*2-r:
y2 = y + r
else:
y2 = w*2 - 1
if z >= r:
z1 = z - r
else:
z1 = 0
if z < w*2-r:
z2 = z + r
else:
z2 = w*2 - 1
b[x,y,z] = np.sum(a[x1:x2,y1:y2,z1:z2])
return b
EDIT: Your original code has a small bug in it. The way numpy indexing works, the final line should be
b[x,y,z] = np.sum(a[x1:x2+1,y1:y2+1,z1:z2+1])
unless you want the cube to be off-centre.
Assuming you do want the cube to be centred, then a much faster way to do this calculation is using scipy's uniform filter:
from scipy.ndimage import uniform_filter
def sum_cubes_quickly(a,b,w,r):
b = uniform_filter(a,mode='constant',cval=0,size=2*r+1)*(2*r+1)**3
return b
A few quick runtime comparisons for randomly generated data with w = 50, r = 10:
Original raw numpy code - 15.1 sec
Numba'd numpy code - 8.1 sec
uniform_filter - 13.1 ms

Generating random values from a customized distribution

I have this distribution below:
using Distributions
struct OrthoNNDist <: DiscreteMultivariateDistribution
x0::Vector{Int64}
oc::Array{Int64,2}
x1s::Array
prob::Float64
#return a new uniform distribution with all vectors in x1s orthogonal to oc
function OrthoNNDist(x0::Vector{Int}, oc::Array{Int,2})
x1s = []
for i = 1:size(oc)[2]
x1 = x0 + oc[:, i]
if nonneg(x1)
push!(x1s, x1)
end
x1 = x0 - oc[:, i]
if nonneg(x1)
push!(x1s, x1)
end
end
new(x0, oc, x1s, 1.0/length(x1s))
end
end
Base.length(d::OrthoNNDist) = length(d.x0)
Distributions.rand(d::OrthoNNDist, N::Integer=1) = rand(d.x1s, 1)
Distributions.pdf(d::OrthoNNDist, x::Vector) = x in d.x1s ? D.prob : 0.0
Distributions.pdf(d::OrthoNNDist) = fill(d.prob, size(d.x1s))
Distributions.logpdf(d::OrthoNNDist, x::Vector) = log(PDF(d, x))
and I want to generate random values from it, I don't know how I've tried:rand(OrthoNNDist,1000)and it didn't work, I'm kinda new on probabilistic programming, I don't know how I can do that.
The function nonneg is no longer provided, that is easy enough to remedy:
nonneg(x::Real) = zero(x) <= x
nonneg(x::Vector{<:Real}) = all(nonneg, x)
Almost all of what you wrote is fine:
using Distributions
struct OrthoNNDist <: DiscreteMultivariateDistribution
x0::Vector{Int64}
oc::Array{Int64,2}
x1s::Array
prob::Float64
#return a new uniform distribution with all vectors in x1s orthogonal to oc
function OrthoNNDist(x0::Vector{Int}, oc::Array{Int,2})
x1s = []
for i = 1:size(oc)[2]
x1 = x0 + oc[:, i]
if nonneg(x1)
push!(x1s, x1)
end
x1 = x0 - oc[:, i]
if nonneg(x1)
push!(x1s, x1)
end
end
new(x0, oc, x1s, 1.0/length(x1s))
end
end
Base.length(d::OrthoNNDist) = length(d.x0)
Distributions.pdf(d::OrthoNNDist, x::Vector) = x in d.x1s ? D.prob : 0.0
Distributions.pdf(d::OrthoNNDist) = fill(d.prob, size(d.x1s))
Distributions.logpdf(d::OrthoNNDist, x::Vector) = log(PDF(d, x))
To get rand working .. you are almost there
using Distributions: rand
Distributions.rand(d::OrthoNNDist, n::Int=1) = rand(d.x1s, n)
now with some data
julia> x0 = rand(1:1_000_000,5);
julia> oc = reshape(rand(1:1_000_000,25), (5,5));
julia> dist = OrthoNNDist(x0,oc);
julia> Distributions.rand(dist, 4)
4-element Array{Any,1}:
[1330729, 656190, 927615, 470782, 1435138]
[1382946, 1058057, 778316, 488440, 1304526]
[1330729, 656190, 927615, 470782, 1435138]
[1409093, 353679, 454229, 698320, 1271674]
(thanks to Mohamed Tarek for his direction)

MATLAB: Efficient way to find the feasible region of first-order polynomials x, y with unknown coefficents in [-1, 1]

As in the title, in MATLAB, I need the feasible region (bounds of all feasible solutions) of
x_0 + x_1 e_1 + ... + x_n e_n
and
y_0 + y_1 e_1 + ... + y_n e_n
where all unknown e_i are in the interval [-1, 1]. I would prefer the solution to not depend on non-standard 3rd party functions.
Below is my quick-and-dirty attempt, but the complexity grows O(2^n), where n is the number of e_i. Any thoughts?
x0 = 3;
x = [1; -3; 0];
y0 = -1;
y = [3; -2; 4];
% Get all permutations of noise symbol extremities
terms = size(x, 1);
xx = zeros(2^terms, 1);
yy = zeros(2^terms, 1);
for j = 1:2^terms
e = double(bitget(j - 1, 1:terms))';
e(e == 0) = -1;
xx(j) = x0 + sum(x .* e);
yy(j) = y0 + sum(y .* e);
end
k = convhull(xx, yy);
plot(xx(k), yy(k));
% First generate all possible permutations for [-1, 1] for n terms. This is similar to what you have done but uses a matlab function
all_e = de2bi(0:(2^terms-1), terms).';
all_e(all_e == 0) = -1;
% Multiply corresponding values of x and y with those of e
xx = x0 + arrayfun(#(z) sum(x .* all_e(:, z)), 1:(2^terms));
yy = x0 + arrayfun(#(z) sum(y .* all_e(:, z)), 1:(2^terms));
You can read more about the function de2bi here
A method to find the absolute minimum and maximum bounds is as follows:
max_e = double(x >= 0);
min_e = double(~max_e);
max_e(max_e == 0) = -1;
min_e(min_e == 0) = -1;
absMax = x0 + sum(x .* max_e);
absMin = x0 + sum(x .* min_e);
Similarly you could do for y

Quadratic Equation From Points

I need to implement a function that finds the trajectory of a projectile and I have three points - origin, destination and the point of maximum height.
I need to find the correct quadratic function that includes these points.
I'm having a hard time figuring out what to do. Where should I start?
Assume you have your origin and destination on the y-axis,namely x1 and x2. If not you can shift them later.
a*x*x + b*x + c = 0//equation
x1*x2=(c/a);
c = (x1*x2)*a;
x1+x2=(-b/a);
b = (x1+x2)/(-a);
a*((x1+x2)/2)^2 + b*((x1+x2)/2) + c = h//max height
let X=(x1+x2)/2;
a*X*X + ((2*X)/(-a))*X + (x1*x2)*a - h = 0;
Now you can iterate through a=0 until the above equation is true as you have all the values X ,x1 , x2 and h.
double eqn = (-h),a=0;//a=0.Assuming you have declared x1,x2 and X already
while(eqn!=0)
{
a++;
eqn = a*X*X + ((2*X)/(-a))*X + (x1*x2)*a - h;
}
b = (x1+x2)/(-a);
c = (x1*x2)*a;
Thus you got all your coeffecients.

Algorithm to find all line segment intersections given n lines

I'm looking for a algorithm to find all the intersection points given n line segments.
Below is the pseudo code from http://jeffe.cs.illinois.edu/teaching/373/notes/x06-sweepline.pdf
The input S[1 .. n] is an array of line
segments.
label[i] is the label of the ith leftmost endpoint.
sort the endpoints of S from left to right
create an empty label sequence
for i ← 1 to 2n
line ← label[i]
if isLeftEndPoint[i]
Insert(line)
if Intersect(S[line], S[Successor(line)])
return TRUE
if Intersect(S[line], S[Predecessor(line)])
return TRUE
else
if Intersect(S[Successor(line)], S[Predecessor(line)])
return TRUE
Delete(label[i])
return FALSE
Apply the algorithm to the line set below, only one intersection point is checked. What should I do to know the existence of the other 2 intersection points?
line[1] enters
line[2] enters, intersection between line[1] and line[2] is checked.
line[3] enters, intersection between line[2] and line[3] is checked.
line[4] enters, intersection between line[4] and line[1] is checked. Intersection A is found.
line[4] leaves, nothing is checked.
line[1] leaves, nothing is checked.
line[2] leaves, nothing is checked.
line[3] leaves, nothing is checked.
Standard line equation
Ax+By=C
The slope(m) of a line defined by the standard line of equation is
m = -(A/B)
Point-slope line equation
y-y1=m(x-x1)
Substituting m = (-A/B) in the point-slope line equation
y2-y1 = (A/-B)*(x2-x1)
(y2-y1)/(x2-x1) = A/-B
thus:
A = y2-y1
B = x1-x2
C = Ax+By
x = (C-By)/A
y = (C-Ax)/B
Given two lines with equation
A1x1+B1y1=C1 and A2x2+B2y2=C2.
Then the point of intersection between the lines is specified
by the points that make A1x+B1y-C1 = A2x+B2y-C2
A1x+B1y=C1
A2x+B2y=C2
A1B2x+B1B2y=B2C1 (multiply the first equation by B2)
A1B2x+B1B2y-B2C1=0
A2B1x+B1B2y=B1C2 (multiply the second equation by B1)
A2B1x+B1B2y-B1C2=0
Equating the two equations
A1B2x+B1B2y-B2C1=A2B1x+B1B2y-B1C2
A1B2x+B1B2y-B2C1-A2B1x-B1B2y+B1C2=0
A1B2x-B2C1-A2B1x+B1C2=0
A1B2x-A2B1x=B2C1-B1C2
x(A1B2-A2B1)=B2C1-B1C2
x = (B2C1-B1C2)/A1B2-A2B1
A1x+B1y=C1
A2x+B2y=C2
A1A2x+A2B1y=A2C1 (multiply the first equation by A2)
A1A2x+A2B1y-A2C1=0
A1A2x+A1B2y=A1C2 (multiply the second equation by A1)
A1A2x+A1B2y-A1C2=0
Equating the two equations
A1A2x+A2B1y-A2C1=A1A2x+A1B2y-A1C2
A1A2x+A2B1y-A2C1-A1A2x-A1B2y+A1C2=0
A1C2-A2C2=A1B2y-A2B1y
A1B2y-A2B1y=A1C2-A2C2
y(A1B2-A2B1)=A1C2-A2C1
y(A1B2-A2B1)=A1C2-A2C1
y = (A1C2-A2C1)/(A1B1-A2B1)
the denominator in y and in x are the same so
denominator = A1B1-A2B1
thus:
x = (B2C1-B1C2)/denominator
y = (A1C2-A2C1)/denominator
These are the x and y coordinates of the intersection of two lines with points (x1, y1), (x2, y2) and (x3, y3), (x4, y4)
Now for a line segment it's the same but we need to check that the x or y coordinate is in both segments. That means between the x coordinate of both segments with lesser value and the x coordinate of both segments with greater value
This is a C++ program that returns true if the segments intersect and returns false if they don't. If the segments intersect it stores the point of intersection in a variable i.
struct Point
{
float x, y;
};
//p1 and p2 are the points of the first segment
//p3 and p4 are the points of the second segment
bool intersection(Point p1, Point p2, Point p3, Point p4, Point &i)
{
float max1; //x-coordinate with greater value in segment 1
float min1; //x-coordinate with lesse value in segment 1
float max2; //x-coordinate with greater value in segment 2
float min2; //x-coordinate with lesser value in segment 2
float A1 = p2.y - p1.y;
float B1 = p1.x - p2.x;
float C1 = A1 * p1.x + B1 * p1.y;
float A2 = p4.y - p3.y;
float B2 = p3.x - p4.x;
float C2 = A2 * p3.x + B2 * p3.y;
float denom = A1 * B2 - A2 * B1;
if (denom == 0.0) //When denom == 0, is because the lines are parallel
return false; //Parallel lines do not intersect
i.x = (C1 * B2 - C2 * B1) / denom;
i.y = (A1 * C2 - A2 * C1) / denom;
if (p1.x > p2.x)
{
max1 = p1.x;
min1 = p2.x;
}
else
{
max1 = p2.x;
min1 = p1.x;
}
if (p3.x > p4.x)
{
max2 = p3.x;
min2 = p4.x;
}
else
{
max2 = p4.x;
min2 = p3.x;
}
//check if x coordinate is in both segments
if (i.x >= min1 && i.x <= max1 &&
i.x >= min2 && i.x <= max2)
return true;
return false; //Do no intersect, intersection of the lines is not between the segments
}
Now you just need to compare on a loop all the segments and store the intersection point on array.

Resources