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

3 ビュー (過去 30 日間)
Abdullah
Abdullah 2022 年 11 月 19 日
回答済み: DGM 2022 年 11 月 19 日
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)));

回答 (1 件)

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

カテゴリ

Help Center および File ExchangeTime Series Events についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by