size of the left side is 2-by-1 and the size of the right side is 1-by-1001

2 views (last 30 days)
I am encountering an error message saying that "Unable to perform assignment because the size of the left side is 2-by-1 and the size of the right side is 1-by-1001."
But I do not know what is the problem.
Would you please help me figure out the problem?
The function is as following:
function u = f_semi_imp_euler(u0, tend, nsteps, k, m)
dt = tend/double(nsteps);
u = zeros(2, nsteps+1);
u(:,1) = u0;
Ma = [1-(dt^(2))*(k/m) dt; -dt*(k/m) 1];
for i = 1:nsteps
u(:,i+1) = Ma*u(:,i);
end
end
-----------------
The code that I am getting an error message from:
T = 10.0; % Final time until which we compute
N = [1000, 750, 500, 250, 100, 75, 50, 10]; % Number of time steps
k = 5;
m = 0.5;
% Initial values
x0 = 1.0;
v0 = 0.1;
u0 = [x0;v0];
alpha = sqrt(k/m); % Computing alpha for linear solution
% Computing parameters A and B for exact linear solution
A = v0/alpha;
B = x0;
% Computing exact linear solution
u_exact = @(t) [A*sin(alpha*t)+B*cos(alpha*t); v0*cos(alpha*t)-x0*alpha*sin(alpha*t)];
err_semi_imp = zeros(2,length(N));
dts = zeros(1,length(N));
for n = 1:length(N)
taxis = linspace(0, T, N(n)+1);
u_semi_imp_euler = f_semi_imp_euler(u0, T, N(n), k, m);
dts(n) = taxis(2) - taxis(1); % store the time step dt for plotting
% Now compute the errors
err_semi_imp(:,n) = max(abs(u_semi_imp_euler - u_exact(taxis)));
end
----------------
The error message after running the program:
Unable to perform assignment because the size of the left side is 2-by-1 and the size of the right side is 1-by-1001.
Error in
err_semi_imp(:,n) = max(abs(u_semi_imp_euler - u_exact(taxis)));

Answers (1)

DGM
DGM on 19 Nov 2022
I think this is what you're after:
err_semi_imp(:,n) = max(abs(u_semi_imp_euler - u_exact(taxis)),[],2);

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!