Hello all, please am solving naviers 2d for wind speed forecasting but am having error, Arrays have incompatible sizes for this operation. Error in navierstokes (line 31)

2 次查看(过去 30 天)
EDIT: Rearranging your code and changing format so that it can be run here.
%callup code
clear
clc
% Define test problem parameters
Lx = 1000; % Length of domain in x direction (m)
Ly = 1000; % Length of domain in y direction (m)
Nx = 100; % Number of grid points in x direction
Ny = 100; % Number of grid points in y direction
dt = 1; % Time step size (s)
t_final = 3600; % Final simulation time (s)
nu = 0.1; % Kinematic viscosity (m^2/s)
rho = 1.2; % Density of air (kg/m^3)
alpha = 0.1; % Coefficient for advection in x direction
beta = 0.2; % Coefficient for advection in y direction
dx=2
dx = 2
dy=2
dy = 2
u = zeros(Ny, Nx); % Initial u velocity field
v = zeros(Ny, Nx); % Initial v velocity field
% Define test boundary condition functions % Zero velocity at all boundaries bc_func = @(x,y,t) [0, 0, 0, 0];
% Define test wind speed profile function wind_profile_func = @(x,y,t) 10 + 2*cos(2*pi*x/Lx)*sin(2*pi*y/Ly);
% Run simulation %[u, v, x, y, t] = windSpeedForecast(Lx, Ly, Nx, Ny, dt, t_final, nu, rho, alpha
[unew,vnew]=navierstokes(u,v,dx,dy,nu,alpha,beta,dt, 100)
Unrecognized function or variable 'u_star'.

Error in solution>navierstokes (line 29)
u_new = u_star + Dx*(u_star(:,3:end)-2*u_star(:,2:end-1)+u_star(:,1:end-2)) ...
function [u,v] = navierstokes(u,v,dx,dy,nu,alpha,beta,dt,n_iterations) % Function to forecast wind speed using simplified Navier-Stokes equation % Inputs: % - u: initial x-velocity field (2D array) % - v: initial y-velocity field (2D array) % - dx: x-direction grid spacing (scalar) % - dy: y-direction grid spacing (scalar) % - nu: kinematic viscosity coefficient (scalar) % - alpha: coefficient for advection in x-direction (scalar) % - beta: coefficient for advection in y-direction (scalar) % - dt: time step size (scalar) % - n_iterations: number of iterations to perform (integer) % Outputs: % - u: final x-velocity field (2D array) % - v: final y-velocity field (2D array)
% Calculate constants N = size(u); Nx = 100; Ny = 100; ax = alpha*dt/dx; ay = alpha*dt/dy; bx = beta*dt/dx; by = beta*dt/dy; Dx = nu*dt/dx^2; Dy = nu*dt/dy^2;
% Loop over time steps for n = 1:n_iterations % Calculate intermediate velocities u_star = u + ax*(u(:,2:end)-u(:,1:end-1)) ... + by*(u(2:end,:)-u(1:end-1,:)); v_star = v + ay*(v(:,2:end)-v(:,1:end-1)) ... + bx*(v(2:end,:)-v(1:end-1,:));
% Calculate final velocities
u_new = u_star + Dx*(u_star(:,3:end)-2*u_star(:,2:end-1)+u_star(:,1:end-2)) ...
+ Dy*(u_star(3:end,:)-2*u_star(2:end-1,:)+u_star(1:end-2,:));
v_new = v_star + Dx*(v_star(:,3:end)-2*v_star(:,2:end-1)+v_star(:,1:end-2)) ...
+ Dy*(v_star(3:end,:)-2*v_star(2:end-1,:)+v_star(1:end-2,:));
% Update velocities for next time step
u = u_new;
v = v_new;
end

回答(1 个)

Cris LaPierre
Cris LaPierre 2023-4-10
This error can occur when you try to subtract two arrays of differing size.
[1 2] - [1 2 3]
Arrays have incompatible sizes for this operation.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by