how do make a driver code and wire plots of wire positions
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Write matlab program The function U satisfies d^2U/dx^2 = d^2U/dt^2 satisfies the boundary conditions U=0 at x=0 and 1 , t>0 . and the initial conditions U=(1/8)*sin(pi*x) , dU/dt=0 for 0<=x<=1 when t=0 Use the explicit finite-difference formula (2) and a central difference approximation for the derivative condition (second IC), to calculate a solution for x=0 :0.1:1 , t=0:0.1: 5.0 (r=1) and for x= 0 : 0.01 : 1 , r=1.0 to t = 5. 0. Plot the solutions . Confirm that the analitical solution is U = (1/ 8 ) sin(pi*x) cos(pi*t) and compare with the numerical solution at several points, say at t= 1. 0 for x = 0. 2 and x =. 5 in both cases (h = 0. 1, h = 0. 01). Provide a printout of your codes, wave code and/or driver if you have one, the approximate solution values at time t = 1. 0 for the step size 0.1, but not for any other time values or step sizes. Do not provide printouts of any other output numbers, provide printouts of the approximate solutions, wire plots of wire positions, at t = 0. 5 and at t = 1. 0 for both cases and the 3-D plots of the compiled solutions up to t = 5. 0 for both cases. Provide Copies of your finite difference code and driver code if one was implemented and copies of your initial (IC) value function m-files, that is, for f(x) and g(x). 2. Printouts of the wire plots in each case at the indicated times, and for the full 3-D plot of resolutions of the solution, as a mesh plot, up to time 5 for both cases (step sizes.) Do not print out the intermediate values or provide printouts of all values computed. Print out the values at time 1 for the step size 0.1, but not for any others. The plots will indicate what is working or not
I got 2 same plote with different t is that right?
how could I meke a driver code
% Define the parameters
L = 1; % length of the domain
T = 5; % final time
dx = 0.01; % grid spacing in x
dt = 0.01; % time step
r = dt^2/dx^2; % Courant number
x = 0:dx:L; % grid points in x
t = 0:dt:T; % grid points in t
n = length(x)-2; % number of interior points
m = length(t); % number of time steps
% Set up the initial conditions
U = zeros(n+2,m);
U(:,1) = (1/8)*sin(pi*x)';
U(:,2) = U(:,1);
% Apply the boundary conditions
U(1,:) = 0;
U(n+2,:) = 0;
% Compute the solution using the explicit finite-difference method
for j = 3:m
for i = 2:n+1
U(i,j) = 2*(1-r)*U(i,j-1) + r*(U(i+1,j-1) + U(i-1,j-1)) - U(i,j-2);
end
end
% Plot the solutions
figure
subplot(1,2,1)
[X,T] = meshgrid(x,t);
surf(X,T,U')
xlabel('x')
ylabel('t')
zlabel('U')
title('Solution with x = 0:0.1:1, t = 0:0.1:5.0')
subplot(1,2,2)
x2 = 0:0.01:1;
U2 = zeros(length(x2),m);
U2(:,1) = (1/8)*sin(pi*x2)';
U2(:,2) = U2(:,1);
n2 = length(x2)-2;
for j = 3:m
for i = 2:n2+1
U2(i,j) = 2*(1-r)*U2(i,j-1) + r*(U2(i+1,j-1) + U2(i-1,j-1)) - U2(i,j-2);
end
end
[X2,T2] = meshgrid(x2,t);
surf(X2,T2,U2')
xlabel('x')
ylabel('t')
zlabel('U')
title('Solution with x = 0:0.01:1, t = 0:0.01:5.0')
% Define the analytical solution
U_analytical = @(x,t) (1/8)*sin(pi*x)*cos(pi*t);
% Define the grid spacings
h1 = 0.1;
h2 = 0.01;
% Evaluate the numerical and analytical solutions at t=1.0 and x=0.2
i1 = round(0.2/h1 + 1); % index corresponding to x=0.2 with h1
i2 = round(0.2/h2 + 1); % index corresponding to x=0.2 with h2
j1 = round(1.0/h1 + 1); % index corresponding to t=1.0 with h1
j2 = round(1.0/h2 + 1); % index corresponding to t=1.0 with h2
U_numeric1 = U(i1,j1);
U_numeric2 = U2(i2,j2);
U_analytical1 = U_analytical(0.2,1.0);
U_analytical2 = U_analytical(0.2,1.0);
fprintf('Results for x=0.2, t=1.0:\n')
fprintf('h=%0.2f: Numerical solution = %0.8f, Analytical solution = %0.8f\n', h1, U_numeric1, U_analytical1)
fprintf('h=%0.2f: Numerical solution = %0.8f, Analytical solution = %0.8f\n', h2, U_numeric2, U_analytical2)
% Evaluate the numerical and analytical solutions at t=1.0 and x=0.5
i1 = round(0.5/h1 + 1); % index corresponding to x=0.5 with h1
i2 = round(0.5/h2 + 1); % index corresponding to x=0.5 with h2
U_numeric1 = U(i1,j1);
U_numeric2 = U2(i2,j2);
U_analytical1 = U_analytical(0.5,1.0);
U_analytical2 = U_analytical(0.5,1.0);
fprintf('Results for x=0.5, t=1.0:\n')
fprintf('h=%0.2f: Numerical solution = %0.8f, Analytical solution = %0.8f\n', h1, U_numeric1, U_analytical1)
fprintf('h=%0.2f: Numerical solution = %0.8f, Analytical solution = %0.8f\n', h2, U_numeric2, U_analytical2)
Results for x=0.2, t=1.0:
h=0.10: Numerical solution = 0.00750277, Analytical solution = -0.07347316
h=0.01: Numerical solution = -0.07347316, Analytical solution = -0.07347316
Results for x=0.5, t=1.0:
h=0.10: Numerical solution = 0.01869218, Analytical solution = -0.12500000
h=0.01: Numerical solution = -0.12500000, Analytical solution = -0.12500000
% Set up the problem parameters
L = 1; % length of x interval
T = 5; % length of t interval
Nx = 11; % number of grid points in x
Nt = 51; % number of grid points in t
dx = L/(Nx-1); % grid spacing in x
dt = T/(Nt-1); % grid spacing in t
r = dt/(dx^2); % stability parameter
% Initialize the solution matrix
U = zeros(Nx,Nt);
% Set the initial conditions
U(:,1) = (1/8)*sin(pi*(0:dx:L))';
% Apply the boundary conditions
U(1,:) = 0;
U(end,:) = 0;
% Apply the explicit finite difference scheme
for j = 2:Nt
for i = 2:Nx-1
U(i,j) = 2*(1-r)*U(i,j-1) + r*(U(i+1,j-1) + U(i-1,j-1)) - U(i,j-2);
end
end
Index in position 2 is invalid. Array indices must be positive integers or logical values.
% Plot the solution at t=0.5
figure
x = 0:dx:L;
plot(x,U(:,round(0.5/dt)+1))
title('Numerical solution at t=0.5')
xlabel('x')
ylabel('U')
% Plot the solution at t=1.0
figure
x = 0:dx:L;
plot(x,U(:,round(1.0/dt)+1))
title('Numerical solution at t=1.0')
xlabel('x')
ylabel('U')
% Plot the 3D wireframe of the solution
figure
[X,T] = meshgrid(0:dx:L,0:dt:T);
mesh(X,T,U')
xlabel('x')
ylabel('t')
zlabel('U')
title('Numerical solution')
could any one help me to continue my program proplem ?
0 Kommentare
Antworten (0)
Siehe auch
Kategorien
Mehr zu Visual Exploration finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!