MATLAB Answers

ERROR came as "Index exceeds matrix dimensions".

1 ビュー (過去 30 日間)
MINATI PATRA
MINATI PATRA 2019 年 8 月 18 日
コメント済み: MINATI PATRA 2019 年 8 月 24 日
%% When I incorporate 3 eqns, ERROR came as "Index exceeds matrix dimensions".
%%Here I have initial condition f=[0 0 0 ], BC: gl=[coswt t t], gr =[0 0 0 ].
What next?
Here is my trial:
H=10;R=5;Pr=1;G1=5;G2=5;Kc=1;Sc=0.22;wt=pi/2;Q=H-(R/Pr);
xl=0; xr=5; % x domain [xl,xr]
J = 10; % J: number of division for x
dx = (xr-xl)/ J; % dx: mesh size
tf = 01; % final simulation time
Nt = 100; % Nt: number of time steps
dt = tf/Nt;
mu = dt/(dx)^2;
% Evaluate the initial conditions
x = xl : dx : xr; % generate the grid point
% f(1:J+1) since array index starts from 1
f1 = 0;f2 = 0;f3 = 0; %%%I.C
% store the solution at all grid points for all time steps
u = zeros(J+1,Nt);
v = zeros(J+1,Nt);
w = zeros(J+1,Nt);
U=[u; v; w];
% Find the approximate solution at each time step
for n = 1:Nt
t = n*dt; % current time
% boundary condition at left side
gl = [cos(wt); t; t];
% boundary condition at right side
gr = [0; 0; 0];
if n==1 % first time step
for j=2:J % interior nodes
u(j,n) = (1+dt*Q)*f1(j) + dt*(G1*f2(j)+G2*f3(j))+mu*(f1(j+1)-2*f1(j)+f1(j-1));
v(j,n) = (1+dt*Q)*f2(j) + (mu/Pr)*(f2(j+1)-2*f2(j)+f2(j-1));
w(j,n) = (1-dt*Kc)*f3(j) + (mu/Sc)*(f3(j+1)-2*f3(j)+f3(j-1));
U=[u(j,n) v(j,n) w(j,n)];
end
U(1,n) = gl; % the left-end point
U(J+1,n) = gr; % the right-end point
else
for j=2:J % interior nodes
u(j,n)= (1+dt*Q)*u(j,n-1)+ dt*(G1*v(j,n-1)+G2*w(j,n-1))+ mu*(u(j+1,n-1)-2*u(j,n-1)+u(j-1,n-1));
v(j,n)= (1+dt*Q)*v(j,n-1)+ (mu/Pr)*(v(j+1,n-1)-2*v(j,n-1)+v(j-1,n-1));
w(j,n)= (1+dt*Q)*w(j,n-1)+ (mu/Sc)*(w(j+1,n-1)-2*w(j,n-1)+w(j-1,n-1));
U=[u(j,n) v(j,n) w(j,n)];
end
U(1,n) = gl; % the left-end point
U(J+1,n) = gr; % the right-end point
end
end
% Plot the results
tt = dt : dt : Nt*dt;
figure(1)
plot(x,u)
hold on
  1 件のコメント
madhan ravi
madhan ravi 2019 年 8 月 18 日

サインインしてコメントする。

回答 (3 件)

Adam Danz
Adam Danz 2019 年 8 月 18 日
編集済み: Adam Danz 2019 年 8 月 18 日
f1 = 0; % 12th line of your code; so, f1 only has 1 element
% (later in your code...)
for j=2:J % interior nodes
u(j,n) = (1+dt*Q)*f1(j) + dt*(G1*f2(j)+G2*f3(j))+mu*(f1(j+1)-2*f1(j)+f1(j-1));
% ^^^^
When j = 2, you're trying to reference the 2nd element of f1 but f1 only has 1 element.
  13 件のコメント
Walter Roberson
Walter Roberson 2019 年 8 月 19 日
% boundary condition at left side
gl = [cos(wt); t; t];
What is that intended to designate?
Is the intention that the first row should be assigned the boundary condition cos(wt) (the first entry) and that the last row should be assigned the boundary condition t (the last entry), and that all other rows should be assigned the boundary condition t (the middle entry) ?

サインインしてコメントする。


MINATI PATRA
MINATI PATRA 2019 年 8 月 20 日
Dear Walter
Please see Equation (12), (Attached pdf)
2nd line is for gl
3rd line is for gr
  8 件のコメント
MINATI PATRA
MINATI PATRA 2019 年 8 月 24 日
Its not working

サインインしてコメントする。


MINATI PATRA
MINATI PATRA 2019 年 8 月 21 日
u, θand ϕ are coded in u, v and w respectively.

Community Treasure Hunt

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

Start Hunting!

Translated by