I am trying to plot the first row of my matrix against time t but I cannot figure out why my matrix yields the error: "vertcat: Dimensions of matrices being concatenated are not consistent."
t = linspace(0,100);
y_mat = (1./t).*([1, t+(1/2)*exp(-3*t)-(1/2)*exp(-t);
(3/2)*(exp(-t)-exp(-3*t)), 1-(3/2)*exp(-3*t)+
(1/2)*exp(-t)] * [(t-4)/3;1]);
plot(t,y_mat(1,:))
You are thinking in the term of symbolic notation but implementing in matrix notation. When you do t = linspace(0,100);
it creates a 1x100
matrix (array). So when later on it is used in the definition of y_mat
, each expression used in the definition evaluates to 1x100
matrix. So your y_mat
definition is tying to do this : [1x100] * [1 1x100 ; 1x100 1x100] * [1x100 ; 1]
[1x100] * [1 1x100 ; 1x100 1x100] * [1x100 ; 1]
[1x100] * [1 1x100 ; 1x100 1x100] * [1x100 ; 1]
which obviously fails.
You have two options: Do all computations in the matrix notation by first computing the matrix multiplication separately and restructuring the matrices to represent the actual multiplication (ensure the 1
s are appropriately replicated).
OR
use Matlabs's symbolic variables and expressions probably like this :
syms t % creating symbolic variable
% creating symbolic expressions
f0 = 1/t
f1 = t+(1/2)*exp(-3*t)-(1/2)*exp(-t);
f2 = (3/2)*(exp(-t)-exp(-3*t));
f3 = 1-(3/2)*exp(-3*t)+(1/2)*exp(-t);
f4 = (t-4)/3;
% defining y_mat
y_mat = f0 * [1 f1; f2 f3] * [f4 ; 1]
% putting value in symbolic variable
t = linspace(eps,100); % eps to avoid division by 0 error
% substitute values and evaluate y_mat
y_mat_vals = eval(subs(y_mat));
This gives y_mat_vals
a 2x100
matrix, as the answer.
YOu have messed up your code..you need to be careful when typing such functions. To make it simple, I have used a loop.
t = linspace(0,100);
nt = length(t) ;
y_mat = zeros(2,nt) ;
for i = 1:nt
y_mat(:,i) = (1/t(i))*([1 t(i)+(1/2)*exp(-3*t(i))-(1/2)*exp(-t(i));
(3/2)*(exp(-t(i))-exp(-3*t(i))) 1-(3/2)*exp(-3*t(i))+(1/2)*exp(-t(i))])*[(t(i)-4)/3;1];
end
plot(t,y_mat)
You can also write it out more explicitly. The equation reads:
[ 1,pt2 ; pt3,pt4 ] * [ pt5 ; 1 ] = [ pt5 + pt2 ; pt3.*pt5 + pt4 ]
Since each of those terms is a scalar, you can compute them for all t
at the same time using element-wise multiplication:
t = linspace(0,100);
pt2 = t+(1/2)*exp(-3*t)-(1/2)*exp(-t);
pt3 = (3/2)*(exp(-t)-exp(-3*t));
pt4 = 1-(3/2)*exp(-3*t)+(1/2)*exp(-t);
pt5 = (t-4)/3;
y_mat = (1./t) .* [ pt5 + pt2 ; pt3.*pt5 + pt4 ];
plot(t,y_mat)
This might be a bit more verbose, but I don't think it's any less readable than other solutions. And it is much more efficient: 0.0571 ms, versus 483.3 ms ( syms solution ) and 0.681 ms ( loop solution) , for a t
with 500 elements.
(Note that multiplying by 1./t
uses implicit singleton expansion. This works in MATLAB R2016b and newer. For older versions of MATLAB, use bsxfun
.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.