Dobra juz widze, ze ten chatgpt to jakis bot. Jak go poprosic o zrobienie przykladu? — Adas1994 10 minut temu
Here is an example of how you might write a function to solve the one-dimensional wave equation in MATLAB:
function [u] = wave_eqn(L, T, c, f, g, N, M)
% WAVE_EQN Solves the one-dimensional wave equation using the finite difference method.
% L: Length of the domain (0 <= x <= L)
% T: Length of the time interval (0 <= t <= T)
% c: Wave speed
% f: Initial displacement
% g: Initial velocity
% N: Number of spatial steps
% M: Number of time steps
% Spatial step size
h = L / N;
% Time step size
k = T / M;
% Preallocate arrays to store the solution at each time step
u = zeros(N+1, M+1);
% Set initial conditions
u(:, 1) = f(0:h:L);
u(:, 2) = u(:, 1) + k * g(0:h:L);
% Set boundary conditions
u(1, :) = 0;
u(N+1, :) = 0;
% Iterate over time
for j = 2:M
for i = 2:N
u(i, j+1) = 2 * (1 - c^2 * k^2 / h^2) * u(i, j) - u(i, j-1) + c^2 * k^2 / h^2 * (u(i+1, j) + u(i-1, j));
end
end
end
To use this function, you would call it with the desired values for L, T, c, f, g, N, and M, and it will return the solution u as an array. For example:
L = 1;
T = 0.5;
c = 1;
f = @(x) sin(pi*x);
g = @(x) cos(pi*x);
N = 10;
M = 50;
u = wave_eqn(L, T, c, f, g, N, M);
This would solve the wave equation on the domain 0 <= x <= 1 for 0 <= t <= 0.5
, with a wave speed of 1, initial displacement given by f(x) = sin(pi*x)
, initial velocity given by g(x) = cos(pi*x)
, using 10 spatial steps and 50 time steps. The solution u would be a 11x51 array, where u(i, j)
is the solution at spatial point i and time j.