cant mutplaye matrix by vector
1 次查看(过去 30 天)
显示 更早的评论
heloo i have this code
clc;
clear all;
R=5;
C=100e-6;
L=10e-3;
U=5;
A=[0 1/C;-1/L -R/L];
x=[0;0];
t=linspace(0,0.5,4);
t_delta=t(2)-t(1);
x_0=[0;0];
for i=1:length(t)
x(:,i+1)=t(i)*A
end
i have this code and i want to multipay A by t,but i get this eror: Assignment has more non-singleton rhs dimensions than non-singleton subscripts
how do i fix it ?
2 个评论
Guillaume
2018-1-4
how do i fix it ?
We don't know since we have no idea what you're trying to do.
t(i)*A results in a 2x2 matrix that you're trying to put in a 2x1 section of a matrix. That's never going to work.
In addition, as commented in your previous question, growing the x matrix is a bad idea. In this particular case, it's not even clear that the loop is needed.
回答(2 个)
Jan
2018-1-4
Your A is 2x2 matrix and x is a 2x1 vector. Then:
x(:,i+1)=t(i)*A
has a 2x2 matrix on the right and a 2x1 vector on the left side.
It is not clear what you want to calculate. Start with writing it down with pencil and paper. Then create a clear an meaningful comment in the code, which explains, what you want to achieve. Afterwards writing the code (or suggesting an improvement) is much easier.
4 个评论
Jan
2018-1-4
t(i) is a scalar, A is a 2x2 matrix. Multiplying them creates a 2x2 matrix. If you state, that you want to get a 2x1 vector, a multiplication is not the right operation.
Star Strider
2018-1-4
If you are doing a state-space simulation, you are doing it incorrectly. You need to use a matrix exponential (the expm function), with the correct matrix multiplications.
See if this does what you want:
for i=1:length(t)
x(:,i) = expm(t(i)*A) * [1; 1] + x_0
end
You need to include the ‘A’, ‘B’, and ‘C’ matrices. (I created ‘B’ as [1;1] to produce a column vector output for ‘x’. Use your own matrices in the calculation.)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!