Error using reshape-I'm not sure whats causing the issue
1 view (last 30 days)
Show older comments
I am currently working on this project where I have to (1) convert the differential equation of motion to the state-space matrix equations using the linear system theory. (2) Plot the step response of the open-loop system using Matlab. Evaluate the dynamic response of
the system. (3) Plot the step response of the design of your close-loop system with the provided controller.
Ive gotten as far as step 2, and Im trying to plot it but no matter what i do i keep getting the same error:
Error using reshape
Number of elements must not change. Use [] as one of the size
inputs to automatically calculate the appropriate size for that
dimension.
Error in symengine>@(t)reshape([0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,(sqrt(1.0e+1).*sin(sqrt(1.0e+1).*t.*(7.0./2.0e+1)))./7.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,cos(sqrt(1.0e+1).*t.*(7.0./2.0e+1)).*(-2.0e+1./4.9e+1)+2.0e+1./4.9e+1],[5,4])
Error in (line 27)
plot(t,zeta(t))
I dont understand what this means, the plot portion of the code was provided for the project, what would be causing this?
clear all; clc;
M = 2; %kg
m = 0.5; %kg
L = 0.5; %m
% xmatrix = [x theta; xdot thetadot]
MM=[M+m,m*L;m*L,m*L^2];
K=[0,0;0,m*9.8*L];
am = (inv(-M)).*K;
OO = zeros(size(eye(2)));
I = eye(2);
A = [OO I ; am OO];
B =[0; 0; 0; 0;inv(M)];
D = [0 1 0 0];
syms G s
Gs=D*(inv(s.*eye(4)-A)).*B;
zeta=matlabFunction(ilaplace(Gs/s));
t=1:0.01:3;
plot(t,zeta(t))
% K=[0.2349 15.53 0.3171 -0.5668];
% Gcs=simplify(D*inv(s*eye(4)-A+B*K)*B)
0 Comments
Answers (2)
KSSV
on 7 Apr 2022
Your zeta is a function handle which gives you 4x4 matrix. You cannot plot it using plot.
Check:
t=1:0.01:3;
for i = 1:length(t)
zeta(t(i))
end
Walter Roberson
on 7 Apr 2022
@(t)reshape([0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,(sqrt(1.0e+1).*sin(sqrt(1.0e+1).*t.*(7.0./2.0e+1)))./7.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,cos(sqrt(1.0e+1).*t.*(7.0./2.0e+1)).*(-2.0e+1./4.9e+1)+2.0e+1./4.9e+1],[5,4])
in order for that reshape() to work, then t would have to be a scalar.
t=1:0.01:3;
but it is not a scalar. When you matlabFunction and pass in a vector variable, the t in the generated function is going to expand to the complete variable content.
In particular, matlabFunction is not going to automatically create code that does
@(t)reshape([zeros(1,1,numel(t)),zeros(1,1,numel(t)),zeros(1,1,numel(t)),zeros(1,1,numel(t)),zeros(1,1,numel(t)),zeros(1,1,numel(t)),zeros(1,1,numel(t)),zeros(1,1,numel(t)),zeros(1,1,numel(t)),(sqrt(1.0e+1).*sin(sqrt(1.0e+1).*reshape(t,1,1,[]).*(7.0./2.0e+1)))./7.0,zeros(1,1,numel(t)),zeros(1,1,numel(t)),zeros(1,1,numel(t)),zeros(1,1,numel(t)),zeros(1,1,numel(t)),zeros(1,1,numel(t)),zeros(1,1,numel(t)),zeros(1,1,numel(t)),zeros(1,1,numel(t)),cos(sqrt(1.0e+1).*reshape(t,1,1,[]).*(7.0./2.0e+1)).*(-2.0e+1./4.9e+1)+2.0e+1./4.9e+1],5,4,[])
to create a 5 x 4 x length(t) array of output.
1 Comment
Walter Roberson
on 7 Apr 2022
M = 2; %kg
m = 0.5; %kg
L = 0.5; %m
% xmatrix = [x theta; xdot thetadot]
MM=[M+m,m*L;m*L,m*L^2];
K=[0,0;0,m*9.8*L];
am = (inv(-M)).*K;
OO = zeros(size(eye(2)));
I = eye(2);
A = [OO I ; am OO];
B =[0; 0; 0; 0;inv(M)];
D = [0 1 0 0];
syms G s
Gs=D*(inv(s.*eye(4)-A)).*B
Z = ilaplace(Gs/s)
Your Gs is an array, so your ilaplace() is going to be an array.
Does it make sense to plot() an array?
See Also
Categories
Find more on Calculus in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!