How can I specify the boundaries of a variable?

19 ビュー (過去 30 日間)
redman
redman 2021 年 11 月 18 日
回答済み: Sameer 2024 年 2 月 20 日
I'm trying to make the range of x as follows
u(x,0) =
In the following code where x is commented as ' X variable'
function [u, q] = Wave(f1,f2,g0,g1,xspan,tspan,nx,nt,alpha)
x0 = xspan(1)
xf = xspan(2)
t0 = tspan(1)
tf = tspan(2)
dx = (xf - x0)/nx;
dt = (tf-t0)/nt;
x = [0:nx]'*dx; %%X variable
t = [0:nt]*dt;
q = alpha*(dt/dx)^2;
q1 = q/2;
q2 = 2*(1-q);
u(:,1) = f1(x);
for k = 1:nt+1, u([1 nx+1],k) = [g0(t(k)); g1(t(k))]; end
u(2:nx,2) = q1*u(1:nx-1,1) + (1-q)*u(2:nx,1) + q1*u(3:nx+1,1) + dt*f2(x(2:nx));
for k = 3:nt+1
u(2:nx,k) = q*u(1:nx-1,k-1) + q2*u(2:nx,k-1) + q*u(3:nx+1,k-1) - u(2:nx,k-2);
end
surf(t,x,u)
xlabel('t')
ylabel('x')
zlabel('u(x,t)')
end
The script I am using in relation to the function is:
clc
clear all
f1 = @(x) 0; %% 'F1 variable'
f2 = @(x) x.^2.*((sin(4.*x)).^2);
g0 = @(t) 0;
g1 = @(t) 0;
xspan = [0,2*pi];
tspan = [0,1];
nx = 20;
nt = 40;
alpha = 4;
[u,r] = Wave(f1,f2,g0,g1,xspan,tspan,nx,nt,alpha);
So in the script the above condition impacts the 'f1 variable' where i equated it to 0 which i know is incorrect but I wanted the code to run . I am not sure how to make the 'x variable' in the function go between the limits stated above.

回答 (1 件)

Sameer
Sameer 2024 年 2 月 20 日
Hi,
From my understanding, you are attempting to simulate the behaviour of a wave over time using a numerical solution to the wave equation in MATLAB. Your goal is to set the initial condition of the wave function u(x,0) such that it equals 1 within a specific range of the spatial domain (pi - 1 <= x <= pi + 1) and 0 elsewhere. However, you are not sure about how to implement this piecewise initial condition within the MATLAB function that you have written.
To implement the initial condition u(x,0)=1 for pi-1 <= x <= pi+1 and 0 otherwise, you need to modify the function f1(x) that represents the initial condition u(x,0) in your script. You can do this by defining ‘f1’ as an anonymous function that applies the given condition to the input x.
Here's how you can modify the ‘f1’ variable in your script:
f1 = @(x) double((x >= pi - 1) & (x <= pi + 1));
This function returns 1 when x is between pi - 1 and pi + 1, and 0 otherwise. The double function is used to ensure that the result is a floating-point number since logical expressions in MATLAB return logical true or false by default.
The final modified script will be as follows:
clc
clear all
% Define the initial condition function
f1 = @(x) double((x >= pi - 1) & (x <= pi + 1));
% Define the second initial condition derivative function
f2 = @(x) x.^2.*((sin(4.*x)).^2);
% Define the boundary condition functions
g0 = @(t) 0;
g1 = @(t) 0;
% Define the spatial and temporal domains
xspan = [0, 2*pi];
tspan = [0, 1];
% Define the number of spatial points and time points
nx = 20;
nt = 40;
% Define the wave speed squared
alpha = 4;
% Call the Wave function
[u, q] = Wave(f1, f2, g0, g1, xspan, tspan, nx, nt, alpha);
With this modification, the initial condition u(x,0) will be set to 1 within the specified range and 0 elsewhere. The Wave function will then use this initial condition to compute the evolution of u over time and space.
I hope this helps!
Sameer

カテゴリ

Help Center および File ExchangeSetup and Configuration についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by