I am using state-space block along with a mux in simulink. I am having two inputs so after passing through mux block I get a row vector. As state-space block accepts one scalar input so when a row vector is given as an input it doesn't work. Any help would be greatly appreciated.
First you should check the dimensions of your state space matrices A,B,C & D. If matrix B is of nx2 then one can use a mux; where n is the order of the system.
Related
Hello I need help plotting the below equation in matlab.
v=10.0004+10.229*e^(-3*t)*sin(5.196*t-257.856)
here is what I have but I keep getting an error:
t=[0:0.1:2];
v=10.0004+10.229*exp(t)*sin(5.196*t+257.856);
plot(t,v)
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the
number of rows in the second matrix. To perform elementwise multiplication, use '.*'.
Error in example (line 2)
v=10.0004+10.229*exp(t)*sin(5.196*t+257.856);
Because t is a matrix, you cannot simply input it as you would a single variable. You have to access each value individually and calculate a corresponding v, then you store that value and move on. Rinse and repeat for each value.
This can be visualized with a for loop. Get the length of your time variable, which will determine how many values you need to calculate, then let the loop run for the corresponding number of elements. Make sure the loop counter is also used to index each element in v.
t = 0:0.1:2 ;
%For each element (n) in t, create a corresponding one of v.
for n = 1:length(t)
v(n) = 10.0004+10.229*exp(t(n))*sin(5.196*t(n)+257.856);
end
plot(t,v)
As we can interpret from the loop, there is a need to do element-wise (good keyword to remember) multiplication. In other languages, you might HAVE to use the loop method. Luckily in Matlab there is a dedicated operator for this '.*'. Therefore in Matlab you could simply modify your code as follows:
t=[0:0.1:2];
v=10.0004+10.229.*exp(t).*sin(5.196.*t+257.856);
plot(t,v)
Either method gives you the desired plot. The first I included to illustrate the underlying logic of what you're looking to do, and the second to simply it with Matlab's syntax. Hope this helps guide you in the future.
Best of luck out there.
I solved a PDE using Matlab solver, pdepe. The initial condition is the solution of an ODE, which I solved in a different m.file. Now, I have the ODE solution in a matrix form of size NxM. How I can use that to be my IC in pdepe? Is that even possible? When I use for loop, pdepe takes only the last iteration to be the initial condition. Any help is appreciated.
Per the pdepe documentation, the initial condition function for the solver has the syntax:
u = icFun(x);
where the initial value of the PDE at a specified value of x is returned in the column vector u.
So the only time an initial condition will be a N x M matrix is when the PDE is a system of N unknowns with M spatial mesh points.
Therefore, an N x M matrix could be used to populate the initial condition, but there would need to be some mapping that associates a given column with a specific value of x. For instance, in the main function that calls pdepe, there could be
% icData is the NxM matrix of data
% xMesh is an 1xM row vector that has the spatial value for each column of icData
icFun = #(x) icData(:,x==xMesh);
The only shortcoming of this approach is that the mesh of the initial condition, and therefore the pdepe solution, is constrained by the initial data. This can be overcome by using an interpolation scheme like:
% icData is the NxM matrix of data
% xMesh is an 1xM row vector that has the spatial value for each column of icData
icFun = #(x) interp1(xMesh,icData',x,'pchip')';
where the transposes are present to conform to the interpretation of the data by interp1.
it is easier for u to use 'method of line' style to define different conditions on each mesh rather than using pdepe
MOL is also more flexible to use in different situation like 3D problem
just saying :))
My experience is that the function defining the initial conditions must return a column vector, i.e. Nx1 matrix if you have N equations. Even if your xmesh is an array of M numbers, the matrix corresponding to the initial condition is still Nx1. You can still return a spatially varying initial condition, and my solution was the following.
I defined an anonymous function, pdeic, which was passed as an argument to pdepe:
pdeic=#(x) pdeic2(x,p1,p2,p3);
And I also defined pdeic2, which always returns a 3x1 column vector, but depending on x, the value is different:
function u0=pdeic2(x,extrap1,extrap2,extrap3)
if x==extrap3
u0=[extrap1;0;extrap2];
else
u0=[extrap1;0;0];
end
So going back to your original question, my guess would be that you have to pass the solution of your ODE to what is named 'pdeic2' in my example, and depending on X, return a column vector.
I am using SIMULINK and I needed to define a Rotation Matrix 3,3,N where N is the number of Robots which I am trying to simulate. To do that, because I am also using the Simulink Coder I had to define the signal related to this matrix as Variable Size and I had to define the upper-bound in the following way:
The problem is that when I want to use only one robot (I set n_robots to 1) I get the following error.
Cannot initialize dimensions of 'R' of 'test_pos_ctrl_target/rotation matrix to Euler angles' to [3x3x1]. When the number of dimensions of a matrix exceeds 2, the size of the trailing dimension must be greater than 1.
Someone could help me?
thanks a lot.
You can't have the last dimension as 1 because MATLAB treats any matrix of dimension [m,n,1] as [m,n]. See size() returns 1 where matrix dimension should not exist for more details.
Try defining R of size [n_robots,3,3] and then re-arrange the matrix inside your code (I assume you are using a MATLAB Function block).
Let's say I want to model this equation (electrical motor, 6 phases):
Vs = Rs*Is + d/dt*(Ls*Is)
where all variables are matrix, so:
Vs = [va1 vb1 vc1 va2 vb2 vc2]' (column vector)
Is = [ia1 ib1 ic1 ia2 ib2 ic2]' (column vector)
Ls and Rs are 6x6 matrix (constants)
From my point of view the Vs is the input vector and Is is the output vector so I need to rearrange the equation.
I have seen that is not possible in Simulink to feed the Transfer Fcn block with matrix, at least not for a multiple input multiple output system.
Is there a way to realize this on Simulink still using the matrix Ls and Rs without "unpacking" the equation?
Thank you
I would re-arrange your equations in state-space form and use the State-Space block, which is better suited for matrix equations.
Another option is to use basic Simulink blocks, such as Integrator and Gain blocks, with vectorized inputs. I am not 100% sure this will work, but reasonably confident.
You can use the product block with matrices inside Matlab so there should be no problem. It's also possible to use integrator/derivative block (though it's better to avoid using derivative if possible) with a vector input so if you can put your equation with Is as an output there should be no problem.
You can put 2 multiply blocks with your matrices as inputs and the vector you need and you will get a vector for the output like you want.
I'm wondering if it's possible for Neural Network to operate on matrices say I want to:
A(i)=matrix(10,10) -> B(i)=matrix(10,10)
input = vector of matrices, i = sample size
output = vector of matrices
Say I would like to guess an matrix operation transforming matrix into another matrix ie
f(A(i,j))=2*A(i,j)*b
Matlab does not take arrays with dimension >2 in NNtool
Any idea?
Thanks
You can simply convert the arrays into vectors before passing them to NNtool. It won't make a difference for the result of your calculation.
In other words, instead of passing A(:,:,i) to NNtool, you pass reshape(A(:,:,i),[],1). Then you reshape the output into a 10x10 array by using B = reshape(outputOfNNtool,10,10).