Program for specific sequence of Integers - matlab

I am solving steady state heat equation with the boundary condition varying like this 10,0,0,10,0,0,10,0,0,10,0,0,10.... and so on depending upon number of points i select.
I want to construct a matrix for these boundary conditions but unable to specify the logic for the sequence in terms of ith element for a matrix.
i am using mathematica for this however i need the formula only like for odd we can specify 2n+1 and for even 2n , something like this for the sequence 10,0,0,10,0,0,10,0,0,10,....

In MATLAB, it would be
M = zeros(1000, 1);
M(1:3:1000) = 10;
to make a 1000 long vector with such structure. 1:3:1000 is 1,4,7,....

Since you specifically want a mathematical formula let me suggest a method:
seq = PadRight[{}, 30, {10, 0, 0}];
func = FindSequenceFunction[seq]
10/3 (1 + Cos[2/3 \[Pi] (-1 + #1)] + Cos[4/3 \[Pi] (-1 + #1)]) &
Test it:
Array[func, 10]
{10, 0, 0, 10, 0, 0, 10, 0, 0, 10}
There are surely simpler programs to generate this sequence, such as:
Array[10 Boole[1 == Mod[#, 3]] &, 10]
{10, 0, 0, 10, 0, 0, 10, 0, 0, 10}

A way to do this in Mathematica:
Take[Flatten[ConstantArray[{10, 0, 0}, Ceiling[1000/3] ], 1],1000]
Another way
Table[Boole[Mod[i,3]==1]*10, {i,1,1000}]

Related

OR-Tools: how to define node that not possible to visit in VRP/CVRP/VRPTW?

im trying this vrp example
From my understanding, we must have 2d array to define distance matrix between all nodes
But for my demand, there is few nodes wont have distance matrix between, like this graph
The distance matrix array will be:
public final long[][] distanceMatrix = {
{0, 2, 1, 0, 0},
{0, 0, 0, 2, 1},
{0, 1, 0, 0, 0},
{0, 0, 0, 0, 3},
{0, 0, 0, 0, 0},
};
The issue is that for the distance matrix array, we must have int number between all nodes, so for two nodes that dont have direct path between (like A->D) i use 0, but OrTool will understand the cost to travel between those two nodes is zero. and the solver always choose that path to go.
So is there any support from ortool library to declare this kind on directional graph?
Thanks
To forbid an arc you have basically two ways:
Using a Distance Dimension and a value above the vehicle max capacity.
e.g. suppose you have a distance dimension:
transit_callback_index = routing.RegisterTransitCallback(distance_callback)
routing.AddDimension(
transit_callback_index,
0, # no slack for distance, can be use as waiting time for Time dimension
42, # maximum distance a vehicle can travel
True, # force start cumul to zero, i.e. vehicle start a km 0
"Distance"
)
...
As you can see, here 42 is the limit for vehicles SO if in your distance matrix you use 43 no vehicle could take this arc since it is above the vehicle capacity (ed above the upper bound of the CumulVar domain).
Thus you could write your distance matrix as:
public final long[][] distanceMatrix = {
{43, 2, 1,43,43},
{43,43,43, 2, 1},
{43, 1,43,43,43},
{43,43,43,43, 3},
{43, 3,43,43,43},
};
note: your SetArcCost() and AddDimension() can use the same registered evaluator.
You can set the list of allowed next nodes.
For each node you can define a list a allowed nodes (by default all nodes)
e.g. here you have:
next = {
[A, [B, C]],
[B, [E, D]],
[C, [B]],
[D, [E]],
[E, []]}
note(instead of A-E you must use 0-4)
Then you can use:
for node, l in next:
node_index = manager.NodeToIndex(node)
next_indices = [node_index] # need itself in case node is dropped
for next_node in l:
next_indices.append(manager.NodeToIndex(next_node))
routing.NextVar(node_index).SetValues(next_indices)
Instead of 0, I would try to initialize those nodes with an unaffordable cost, eg int64.Maxvalue

How do I fill Matrix4 with translation, skew and scale values in flutter?

Suppose, I have these values for a container of height 200 and width 300:
scaleX = 0.9198
scaleY = 0.9198
skewX = -0.3923
skewY = 0.3923
translateX = 150
translateY = 150
Now, how do I fill this values in Matrix4 correctly?
I tried doing this:
Matrix4(
0.9198, 0, 0, 0, //
0, 0.9198, 0, 0, //
0, 0, 1, 0, //
150, 150, 0, 1,
)
which is,
Matrix4(
scaleX, 0, 0, 0, //
0, scaleY, 0, 0, //
0, 0, 1, 0, //
translateX, translateY, 0, 1,
)
But I am not sure where to put skewX and skewY values in this matrix. Please help me with this.
Skew Values
This is a bit of a nuanced topic, as it could be interpreted in a couple of different ways. There are specific cells of a matrix that are associated with specific names, as identified in your question, translate x, translate y, scale x, and scale y. In this context, you most likely mean the values from a matrix that are called skew x and skew y (also sometimes known as shear x and shear y), which refers to indices 4 and 1 (zero-based, column-major order). They're called these names because when put into an identity matrix by themselves, they do that operation (translate, scale, or skew), but it gets more complicated when there are multiple values.
On the other hand, this could also be interpreted as a series of operations (e.g. scale by (0.9198, 0.9198, 1), then skew by (-0.3923, 0.3923), then translate by (150, 150, 0)), and then it's a series of matrix multiplications that would ultimately result in a similar-looking, but numerically different matrix. I'll assume you don't mean this for this question. You can read more about it here though.
You can consult the Flutter Matrix4 documentation, which also provides implementation notes for Matrix4.skewX and Matrix4.skewY. The skews are stored in (zero-based) indices 4, and 1, as the tangent of the skewed angle.
Matrix4(
scaleX, skewY, 0, 0, // skewY could also be tan(ySkewAngle)
skewX, scaleY, 0, 0, // skewX could also be tan(xSkewAngle)
0, 0, 1, 0, //
translateX, translateY, 0, 1,
)
Note to those that aren't familiar with Flutter's data structures that values are stored in column-major order which means that each row in the above code is actually a column, so if you were to represent the matrix as a normal transformation matrix, it's transposed.
More information:
Transformation Matrices: https://en.wikipedia.org/wiki/Transformation_matrix
How matrices are used with CSS Transforms: How do I use the matrix transform and other transform CSS properties?

How to minimise computation time for a matrix diagonalisation plotting script in matlab?

I am fairly new to matlab and was hoping for some assistance regarding the speeding up of computation and plotting of eigenvalues. I have a 'pentadiagonal' 40x40 matrix, with symbolic k entries.
I wish to compute all of the eigenvalues and plot these as a function of k. So I call eig(H), then I set up a loop which replaces the symbolic k entries with a numerical value. I then simply plot this array, which I have called 'list'.
This routine takes a long time to evaluate, and I do need all of the eigenvalues rather than just the n largest. Does anybody have any advice as to how I can speed up this calculation?
Things I have tried:
1) Preallocating my array. This didn't appear to speed it up much. I believe at this stage, the code still holds the eigenvalues as Root[#1^2+cos(1.0*3)...] objects, and the list is relatively small.
2) Rescaling the arguments of the trig functions cos((sqrt(3)/2)*k) -> cos(k). This also doesn't appear to speed it up a huge amount.
I would be very grateful for any further advice.
a=0.5;
b=0.5;
dq=0.01;
d=0.5;
syms k
H= [(-2)*a - b*d*sin(sqrt(3)*k), 2*a*cos((sqrt(3)/2)*k), b*d*sin((sqrt(3)/2)*k), 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0; 2*a*cos((sqrt(3)/2)*k), -1, ...]
list = [];
for s=0:dq:3
list=[list,subs(energies,k,s)];
end
q=0:dq:3;
plot(q,transpose(list),'red')

Order of connecting points in paraview under table to structured grids

I'm a beginner in Paraview. I have a question about displaying CSV file in Paraview. If my data file looked like this
x coord, y coord, z coord, scalar
0, 0, 0, 1
1, 0, 0, 2
0, 1, 0, 3
1, 1, 0, 4
0, 0, 1, 5
1, 0, 1, 6
0, 1, 1, 7
1, 1, 1, 8
It will create a cubic grid. But if I switch the order of points like
x coord, y coord, z coord, scalar
0, 0, 0, 1
1, 0, 0, 2
1, 0, 1, 6
0, 1, 0, 3
1, 1, 1, 8
1, 1, 0, 4
0, 0, 1, 5
0, 1, 1, 7
It will give me a really messy connected wireframe. I want to know what's the order of connection? How does Paraview form those grids?
In ParaView (and indeed in the underlying VTK library it uses), structured grid points are ordered such that the index of the x dimension varies fastest, the index of the y dimension varies second-fastest, and the index of the z dimension varies slowest. Hence, your first example gives the expected result while the second example does not.

Correct format for loading vertex arrays from file

I've been banging my head on my keyboard for the past couple of weeks over this. What I'm trying to do is load an array of floats (GLfloat) and an array of unsigned shorts (GLushort) from a text file into equivalent arrays in objective-c so that I can render the contained objects. I've got my arrays loaded into vector objects as
vector<float> vertices;
and
vector<GLushort> indices;
But for some reason I can't figure out why I can't get these to render. Here is my code for rendering the above:
glVertexPointer(3, GL_FLOAT, sizeof(vertices[0])*6, &vertices[0]);
glNormalPoitner(GL_FLOAT, sizeof(vertices[0])*6, &vertices[3]);
glDrawElements(GL_TRIANGLES, sizeof(indices)/sizeof(indices[0]), GL_UNSIGNED_SHORT, indices);
Sample arrays are below:
vertices: (Vx, Vy, Vz, Nx, Ny, Nz)
{10, 10, 0, 0, 0, 1,
-10, 10, 0, 0, 0, 1,
-10, -10, 0, 0, 0, 1,
10, -10, 0, 0, 0, 1};
indices: (v1, v2, v3)
{0, 1, 2,
0, 2, 3};
The text file I want to load these arrays from for rendering looks like this:
4 //Number of Vertices
###Vertices###
v 10 10 0 0 0 1
v -10 10 0 0 0 1
v -10 -10 0 0 0 1
v 10 -10 0 0 0 1
###Object1###
2 //Number of faces
f 0 1 2
f 3 4 5
Are vector objects the best approach to take? If not, what is? And what am I doing wrong that these won't render? Thanks.
You use GL_TRIANGLES to specify the vertices format.
See the graph about GL_TRIANGLES, your format is wrong.
And, I prefer to use GL_TRIANGLE_STRIP format. It needs few vertices.