Facing problems in nonlinear system
2 次查看(过去 30 天)
显示 更早的评论
%%%% Problem_01 %%%%
%u_t=u_xx+6u(1-u)%
%u(0,t)=(1+e^(-5t))^(-2)%
%u(1,t)=(1+e^(1-5t))^(-2)%
%u(x,0)=(1+e^x)^(-2)%
clc;
clear all;
format short
L = 1; % Length of the rod
T = 0.05; % Total time
Nx = 7; % Number of spatial steps
Nt = 10; % Number of time steps
alpha = 1; % Thermal diffusivity
dx = L / Nx; % Spatial step size
dt = T / Nt; % Time step size
r = alpha * dt / dx^2
u = sym('u', [Nx+1,Nt+1]);
% Define the spatial grid
x = linspace(0, L, Nx+1);
x
% Set initial condition u(x,0)
u(:, 1) = vpa(0.0001679.*(x.^2-x)+0.00002215.*(1.5.*x.^3-1.5.*x),9);
% Set Dirichlet boundary conditions
u(1, :) = boundary_condition_x0(linspace(0, T, Nt+1)); % u(0,t)
u(end, :) = boundary_condition_xL(linspace(0, T, Nt+1)); % u(1,t)
u;
% Initialize the source term matrix
f = [];
R = [];
for n = 1:Nt
for j = 1:Nx+1
f_expr = (-10*(exp(x(j)-5*(n-0.5)*dt))/(1+exp(x(j)-5*(n-0.5)*dt))^3) -(2*exp(x(j)-5*(n-0.5)*dt)/(1+exp(x(j)-5*(n-0.5)*dt)^3)*((1-2*exp(x(j)-5*(n-0.5)*dt)))/(1+ ...
exp(x(j)-5*(n-0.5)*dt)))-0.0003358-0.00019935*x(j) +6*((1/(1+exp(x(j)-5*(n-0.5)*dt))^2)-0.0001679*(x(j).^2-x(j))-0.00002215*((3/2)*(x(j).^3-x(j))))*(1-(1/(1+ ...
exp(x(j)-5*(n-0.5)*dt))^2) +0.0001679*(x(j).^2-x(j))+0.00002215*((3/2)*(x(j).^3-x(j)))) + 6*0.5*(u(j,n)+u(j,n+1))*(1-0.5*(u(j,n)+u(j,n+1)));
f{j,n} = f_expr;
end
for j = 2:Nx
eq = (1-6*r)*u(j-1, n+1) + (10 + 12*r)*u(j, n+1) + (1 - 6*r)*u(j+1, n+1) == (1 + 6*r)*u(j-1, n)...
+ (10-12*r)*u(j, n) + (1 +6*r)*u(j+1, n) + dt*(f{j-1,n} +10*f{j,n} + f{j+1,n});
eqs(n,j-1) = eq;
end
% disp("Equations before solving:");
% disp(vpa(eqs(n, :), 6));
vsol = vpasolve(eqs(n,:));
R = struct2cell(vsol);
for j = 2:Nx
u(j,n+1) = min(abs(R{j-1}));
end
end
vpa(u,9);
esol = @(x,t) (1+exp(x-5*t))^(-2);
exact_sol = [];
Compact_sol = [];
Error_Compact = [];
for n = 2:Nt+1
for j = 2:Nx+1
exact_sol(j,n) = esol((j-1)*dx,(n-1)*dt);
Compact_sol(j,n) = u(j,n);
Error_Compact(j,n) = abs(exact_sol(j,n)-Compact_sol(j,n));
end
end
n = 11; % Choose any specific value of n (1 to 10)
j_values = 1:Nx+1;
u_values = u(j_values, n);
exact_val = exact_sol(j_values, n);
Compact_val = Compact_sol(j_values,n);
Compact_error_val = Error_Compact(j_values, n);
Table = table(u_values, exact_val,Compact_val,Compact_error_val, ...
'VariableNames', {'E(i,j)', 'Exact_Solution','Compact_Solution','Compact_Error'})
function bc_x0 = boundary_condition_x0(t)% E(0,t)
bc_x0 = zeros(size(t));
end
function bc_xL = boundary_condition_xL(t)% E(1,t)
bc_xL = zeros(size(t));
end
采纳的回答
Torsten
2025-3-28
编辑:Torsten
2025-3-28
"pdepe" gives a different solution than your method. Maybe the nonlinear systems have multiple solutions.
%%%% Problem_01 %%%%
%u_t=u_xx+6u(1-u)%
%u(0,t)=(1+e^(-5t))^(-2)%
%u(1,t)=(1+e^(1-5t))^(-2)%
%u(x,0)=(1+e^x)^(-2)%
L = 1; % Length of the rod
T = 0.05; % Total time
Nx = 500; % Number of spatial steps
Nt = 10; % Number of time steps
alpha = 1; % Thermal diffusivity
dx = L / Nx; % Spatial step size
dt = T / Nt; % Time step size
r = alpha * dt / dx^2;
x = linspace(0, L, Nx+1);
t = linspace(0, T, Nt+1);
% Set initial condition u(x,0)
u = zeros(Nx+1,Nt+1);
u(:,1) = 0.0001679*(x.^2-x)+0.00002215*(1.5*x.^3-1.5*x);
for n = 2:Nt
u(:,n) = fsolve(@(U)fun(U,n-1,Nx,x,dx,t(n),dt,r,u(:,n-1)),u(:,n-1),optimset('Display','none'));
end
figure(1)
plot(x,u)
uic = @(x)0.0001679*(x.^2-x)+0.00002215*(1.5*x.^3-1.5*x);
upde = @(x,t,u,dudx)deal(1,dudx,6*u*(1-u));
ubc = @(xl,ul,xr,ur,t)deal(ul,0,ur,0);
x = linspace(0,L,501);
t = linspace(0,T,11);
m = 0;
sol = pdepe(m,upde,uic,ubc,x,t);
u = sol(:,:,1);
figure(2)
plot(x,u)
function bc_x0 = boundary_condition_x0(t)% E(0,t)
bc_x0 = zeros(size(t));
end
function bc_xL = boundary_condition_xL(t)% E(1,t)
bc_xL = zeros(size(t));
end
function res = fun(U,n,Nx,x,dx,t,dt,r,Uold)
f = zeros(Nx+1,1);
res = zeros(Nx+1,1);
for j = 1:Nx+1
f(j) = (-10*(exp(x(j)-5*(n-0.5)*dt))/(1+exp(x(j)-5*(n-0.5)*dt))^3) -(2*exp(x(j)-5*(n-0.5)*dt)/(1+exp(x(j)-5*(n-0.5)*dt)^3)*((1-2*exp(x(j)-5*(n-0.5)*dt)))/(1+ ...
exp(x(j)-5*(n-0.5)*dt)))-0.0003358-0.00019935*x(j) +6*((1/(1+exp(x(j)-5*(n-0.5)*dt))^2)-0.0001679*(x(j)^2-x(j))-0.00002215*((3/2)*(x(j)^3-x(j))))*(1-(1/(1+ ...
exp(x(j)-5*(n-0.5)*dt))^2) +0.0001679*(x(j)^2-x(j))+0.00002215*((3/2)*(x(j)^3-x(j)))) + 6*0.5*(Uold(j)+U(j))*(1-0.5*(Uold(j)+U(j)));
end
res(1) = U(1) - boundary_condition_x0(t);
for j = 2:Nx
res(j) = (1-6*r)*U(j-1) + (10 + 12*r)*U(j) + (1 - 6*r)*U(j+1) - ((1 + 6*r)*Uold(j-1)...
+ (10-12*r)*Uold(j) + (1 + 6*r)*Uold(j+1) + dt*(f(j-1) +10*f(j) + f(j+1)));
end
res(Nx+1) = U(Nx+1) - boundary_condition_xL(t);
end
6 个评论
Torsten
2025-3-29
编辑:Torsten
2025-3-29
The numerical solution appears to be running correctly. However, in the results table, only the values at the following specific points are required:u(0.1,0.05),u(0.2,0.05),…,u(1,0.05).
Then you should choose Nx such that 0.1, 0.2,..,0.9,1 are part of the grid, i.e. Nx should be a multiple of 10.
is this code run for any nonlinear problems ?
What do you mean by "any nonlinear problems" ?
It solves the Nx+1 nonlinear equations
U(1) - boundary_condition_x0(t) = 0;
(1-6*r)*U(j-1) + (10 + 12*r)*U(j) + (1 - 6*r)*U(j+1) - ((1 + 6*r)*Uold(j-1)...
+ (10-12*r)*Uold(j) + (1 + 6*r)*Uold(j+1) + dt*(f(j-1) +10*f(j) + f(j+1))) = 0
(2 <= j <= Nx)
U(Nx+1) - boundary_condition_xL(t) = 0
for U in each time step.
When i start at 1, it has error... "Index in position 2 is invalid. Array indices must be positive integers or logical values."
I don't get this error. As you can see, it runs without problems with MATLAB online.
By the way: "pdepe" is not able to solve the problem you posted:
L = 1;
T = 0.05;
uic = @(x)0.001679*(x^2-x)+0.0002215*(1.5*x^3-1.5*x);
upde = @(x,t,u,dudx)deal(1,dudx,...
6*u*(1-u)-...
(1+exp(x-5*t))^(-2)-...
0.001679*(x^2-x)-...
0.0002215*(1.5*x^3-1.5*x)*2*exp(x-5*t)*(1-2*exp(x-5*t))/...
((1+exp(x-5*t))^3*(1+exp(x-5*t)))-...
0.003358-0.0019935*x+...
6*((1-exp(x-5*t))^(-2)-...
0.001679*(x^2-x)-0.0002215*(1.5*x^3-1.5*x))*...
(1-(1+exp(x-5*t))^(-2)+...
0.001679*(x^2-x)+0.0002215*(1.5*x^3-1.5*x)));
ubc = @(xl,ul,xr,ur,t)deal(ul,0,ur,0);
x = linspace(0,L,1001);
t = linspace(0,T,11);
m = 0;
sol = pdepe(m,upde,uic,ubc,x,t);
u = sol(:,:,1);
plot(x,u)
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



