Finite Differences using fsolve for non linear equations

조회 수: 34 (최근 30일)
Ellson Chow
Ellson Chow 2020년 8월 21일
댓글: Abdallah Daher 2022년 3월 15일
I need help writing a code that solves the boundary layer problem for fluid mechanics. I am using fsolve to solve non-linear equations (continuity and momentum equations) for the boundary layer, but I am stuck getting no solutions for my code. Any help is greatly appreciated! Here I have attempted to convert my function F (equations) and its variables, u and v, into a single vector (f and x respectively) in an attempt to solve it as I assume that fsolve takes in vector inputs. Total 2*ny*nx equations and hence unknowns to be solved, 1 for each cell.
My code:
clear all, close all, clc
nx = 10;
ny = 10;
x0 = ones(2*ny*nx,1);
k = fsolve(@blayer, x0);
function f = blayer(x)
nx = 10;
ny = 10;
L = 1;
H = 1;
dx = L/(nx-1);
dy = H/(ny-1);
P = 1;
pgrad = P/L;
v = zeros(ny,nx);
u = zeros(ny,nx);
u(:,1) = 1; % Incoming
v(:,1) = 0; % Incoming
u(1,:) = 0; % Bottom
v(1,:) = 0; % Bottom
u(ny,:) = 0; % Top
v(ny,:) = 0; % Top
F = zeros(2*ny, nx);
X = [u;v];
Xt = X';
x = Xt(:);
j = 0;
while j <= ny
j = j+1;
if j == 1
for i = 1:nx
if i == 1
F(j,i) = (u(j,i+1)-u(j,i))/dx + (v(j+1,i)-v(j,i))/dy;
F(j+ny,i) = u(j,i)*(u(j,i+1)-u(j,i))/dx + v(j,i)*(u(j+1,i)-u(j,i))/dy - (u(j+1,i)-2*u(j,i)+u(j,i))/dy^2 + pgrad;
elseif i > 1 && i < nx
F(j,i) = (u(j,i+1)-u(j,i-1))/(2*dx) + (v(j+1,i)-v(j,i))/dy;
F(j+ny,i) = u(j,i)*(u(j,i+1)-u(j,i-1))/(2*dx) + v(j,i)*(u(j+1,i)-u(j,i))/dy - (u(j+1,i)-2*u(j,i)+u(j,i))/dy^2 + pgrad;
elseif i == nx
F(j,i) = (u(j,i)-u(j,i-1))/dx + (v(j+1,i)-v(j,i))/dy;
F(j+ny,i) = u(j,i)*(u(j,i)-u(j,i-1))/dx + v(j,i)*(u(j+1,i)-u(j,i))/dy - (u(j+1,i)-2*u(j,i)+u(j,i))/dy^2 + pgrad;
end
end
end
if j > 1 && j < ny
for i = 1:nx
if i == 1
F(j,i) = (u(j,i+1)-u(j,i))/dx + (v(j+1,i)-v(j-1,i))/(2*dy);
F(j+ny,i) = u(j,i)*(u(j,i+1)-u(j,i))/dx + v(j,i)*(u(j+1,i)-u(j-1,i))/(2*dy) - (u(j+1,i)-2*u(j,i)+u(j-1,i))/dy^2 + pgrad;
elseif i > 1 && i < nx
F(j,i) = (u(j,i+1)-u(j,i-1))/(2*dx) + (v(j+1,i)-v(j-1,i))/(2*dy);
F(j+ny,i) = u(j,i)*(u(j,i+1)-u(j,i-1))/(2*dx) + v(j,i)*(u(j+1,i)-u(j-1,i))/(2*dy) - (u(j+1,i)-2*u(j,i)+u(j-1,i))/dy^2 + pgrad;
elseif i == nx
F(j,i) = (u(j,i)-u(j,i-1))/dx + (v(j+1,i)-v(j-1,i))/(2*dy);
F(j+ny,i) = u(j,i)*(u(j,i)-u(j,i-1))/dx + v(j,i)*(u(j+1,i)-u(j-1,i))/(2*dy) - (u(j+1,i)-2*u(j,i)+u(j-1,i))/dy^2 + pgrad;
end
end
end
if j == ny
for i = 1:nx
if i == 1
F(j,i) = (u(j,i+1)-u(j,i))/dx + (v(j,i)-v(j-1,i))/dy;
F(j+ny,i) = u(j,i)*(u(j,i+1)-u(j,i))/dx + v(j,i)*(u(j,i)-u(j-1,i))/dy - (u(j,i)-2*u(j,i)+u(j-1,i))/dy^2 + pgrad;
elseif i > 1 && i < nx
F(j,i) = (u(j,i+1)-u(j,i-1))/(2*dx) + (v(j,i)-v(j-1,i))/dy;
F(j+ny,i) = u(j,i)*(u(j,i+1)-u(j,i-1))/(2*dx) + v(j,i)*(u(j,i)-u(j-1,i))/dy - (u(j,i)-2*u(j,i)+u(j-1,i))/dy^2 + pgrad;
elseif i == nx
F(j,i) = (u(j,i)-u(j,i-1))/dx + (v(j,i)-v(j-1,i))/dy;
F(j+ny,i) = u(j,i)*(u(j,i)-u(j,i-1))/dx + v(j,i)*(u(j,i)-u(j-1,i))/dy - (u(j,i)-2*u(j,i)+u(j-1,i))/dy^2 + pgrad;
end
end
end
end
Ft = F';
f = Ft(:);
end
  댓글 수: 1
Ellson Chow
Ellson Chow 2020년 8월 21일
I have changed my initial guess to u = 1 and v = 0;
x0 = zeros(2*ny*nx,1);
x0(1:ny*nx) = 1;
but still does not work.

댓글을 달려면 로그인하십시오.

답변 (1개)

Alan Stevens
Alan Stevens 2020년 8월 21일
Do you need fsolve at all? Doesn't your blayer function do the solving itself? Couldn't you simply have
nx = 10;
ny = 10;
L = 1;
H = 1;
dx = L/(nx-1);
dy = H/(ny-1);
P = 1;
pgrad = P/L;
v = zeros(ny,nx);
u = zeros(ny,nx);
u(:,1) = 1; % Incoming
v(:,1) = 0; % Incoming
u(1,:) = 0; % Bottom
v(1,:) = 0; % Bottom
u(ny,:) = 0; % Top
v(ny,:) = 0; % Top
F = zeros(2*ny, nx);
% X = [u;v];
% Xt = X';
% x = Xt(:);
j = 0;
while j <= ny
j = j+1;
if j == 1
for i = 1:nx
if i == 1
F(j,i) = (u(j,i+1)-u(j,i))/dx + (v(j+1,i)-v(j,i))/dy;
F(j+ny,i) = u(j,i)*(u(j,i+1)-u(j,i))/dx + v(j,i)*(u(j+1,i)-u(j,i))/dy - (u(j+1,i)-2*u(j,i)+u(j,i))/dy^2 + pgrad;
elseif i > 1 && i < nx
F(j,i) = (u(j,i+1)-u(j,i-1))/(2*dx) + (v(j+1,i)-v(j,i))/dy;
F(j+ny,i) = u(j,i)*(u(j,i+1)-u(j,i-1))/(2*dx) + v(j,i)*(u(j+1,i)-u(j,i))/dy - (u(j+1,i)-2*u(j,i)+u(j,i))/dy^2 + pgrad;
elseif i == nx
F(j,i) = (u(j,i)-u(j,i-1))/dx + (v(j+1,i)-v(j,i))/dy;
F(j+ny,i) = u(j,i)*(u(j,i)-u(j,i-1))/dx + v(j,i)*(u(j+1,i)-u(j,i))/dy - (u(j+1,i)-2*u(j,i)+u(j,i))/dy^2 + pgrad;
end
end
end
if j > 1 && j < ny
for i = 1:nx
if i == 1
F(j,i) = (u(j,i+1)-u(j,i))/dx + (v(j+1,i)-v(j-1,i))/(2*dy);
F(j+ny,i) = u(j,i)*(u(j,i+1)-u(j,i))/dx + v(j,i)*(u(j+1,i)-u(j-1,i))/(2*dy) - (u(j+1,i)-2*u(j,i)+u(j-1,i))/dy^2 + pgrad;
elseif i > 1 && i < nx
F(j,i) = (u(j,i+1)-u(j,i-1))/(2*dx) + (v(j+1,i)-v(j-1,i))/(2*dy);
F(j+ny,i) = u(j,i)*(u(j,i+1)-u(j,i-1))/(2*dx) + v(j,i)*(u(j+1,i)-u(j-1,i))/(2*dy) - (u(j+1,i)-2*u(j,i)+u(j-1,i))/dy^2 + pgrad;
elseif i == nx
F(j,i) = (u(j,i)-u(j,i-1))/dx + (v(j+1,i)-v(j-1,i))/(2*dy);
F(j+ny,i) = u(j,i)*(u(j,i)-u(j,i-1))/dx + v(j,i)*(u(j+1,i)-u(j-1,i))/(2*dy) - (u(j+1,i)-2*u(j,i)+u(j-1,i))/dy^2 + pgrad;
end
end
end
if j == ny
for i = 1:nx
if i == 1
F(j,i) = (u(j,i+1)-u(j,i))/dx + (v(j,i)-v(j-1,i))/dy;
F(j+ny,i) = u(j,i)*(u(j,i+1)-u(j,i))/dx + v(j,i)*(u(j,i)-u(j-1,i))/dy - (u(j,i)-2*u(j,i)+u(j-1,i))/dy^2 + pgrad;
elseif i > 1 && i < nx
F(j,i) = (u(j,i+1)-u(j,i-1))/(2*dx) + (v(j,i)-v(j-1,i))/dy;
F(j+ny,i) = u(j,i)*(u(j,i+1)-u(j,i-1))/(2*dx) + v(j,i)*(u(j,i)-u(j-1,i))/dy - (u(j,i)-2*u(j,i)+u(j-1,i))/dy^2 + pgrad;
elseif i == nx
F(j,i) = (u(j,i)-u(j,i-1))/dx + (v(j,i)-v(j-1,i))/dy;
F(j+ny,i) = u(j,i)*(u(j,i)-u(j,i-1))/dx + v(j,i)*(u(j,i)-u(j-1,i))/dy - (u(j,i)-2*u(j,i)+u(j-1,i))/dy^2 + pgrad;
end
end
end
end
surf(F)
or am I missing something?
  댓글 수: 3
Alan Stevens
Alan Stevens 2020년 8월 21일
So you want to find the values of u and v that make F zero everywhere? In that case you need to pass u and v to blayer, not x (the values of which are fixed). i.e. you want fsolve to find values of u and v that set all the Fs to zero.
Abdallah Daher
Abdallah Daher 2022년 3월 15일
Were you able it out? I am currently trying to write an explicit finite difference scheme for a flat plate boundary layer

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Condensed Matter & Materials Physics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by