Effacer les filtres
Effacer les filtres

Can someone help meto fix it. Error: File: Untitled.m Line: 42 Column: 16 Unexpected MATLAB expression. Thanks a lots

2 vues (au cours des 30 derniers jours)
% This program implements an explicit scheme to solve the Burgers?Huxley
% eqution, with the addition of shifting the profile. The code ... outputs the
% travelling wave profile, with its corresponding speeds with respect to
% variation in time.
clear all; clc;
% Spacial increment.
dx = 0.1;
% Time increment.
dt = (0.95)*(1/2)*dx^2;
% Initial spacial domain.
x = [-20:dx:20];
% Time frame for simulation.
T = 600;
% Initial conditions.
u = 1/2*(1-tanh(x)) ;
% Parameters k,m and n.
pk = 6; pm = 6; pn = 6;
% Defining initail states of paramters that will be used in the program.
iter = 1;
cumshift = 0;
xoffset = 0;
xpos = 0;
tm = 0;
% Implementing a shift every 300th iteration.
for j = 0:dt:T
% Applying a numerical shift every .
if (mod(iter,300)==0)
figure(1)
% Returns indices of the vector "u" that agree with the condition.
ind = find(u>0.1 & u<0.9);
% Returns the x value that corresponds to the u value at 0.5.
xc = interp1(u(ind),x(ind),0.5);
% Determining the number of steps xc is away from 0.
N = floor(xc/dx);
% Creating shift variable.
xshift = N*dx;
% Cumulated shift.
cumshift = cumshift + xshift;
% Rounding error.
xoffset = xc - xshift;
% Creating shifted u.
u = [u(N+1:end)zeros (1,N)];
% Shifting x domain.
newx = x+cumshift;
% Plotting shifting wave solution.
plot(newx,u,'b');
axis([newx(1) newx(end) -0.1 1.1])
pause(0.01)
% Adding the total shift to a vector.
xpos = [xpos cumshift+xoffset];
% Adding the accumulated time frames between shifts.
tm = [tm j];
end
% Explicit scheme.
u(2:end-1) = u(2:end-1) + ... (dt/(dx)ˆ2).*(u(3:end)?2.*u(2:end?1)+u(1:end?2)) ...
- (dt/(2*dx)).*u(2:end-1).^pk.*(u(3:end) - u(1:end-2)) ...
+ dt.*u(2:end-1).^pm.*(1-u(2:end-1).^pn);
% Boundary conditions.
u(1) = 1;
u(length(x)) = 0;
iter = iter+1;
end
% Defining parameter values.
sum = 0;
k = 0;
speed(1) = 0;
% Computing the speeds with respect to time.
for j = 2:length(xpos)
g = (xpos(j)-xpos(j-1))/(tm(j)-tm(j-1));
sum = sum + g;
k = k+1;
speed(j) = sum/k;
end
figure (4)
% Plotting a Speed vs Time graph.
plot(tm,speed)
fprintf('final speed = %1.9f\n', speed(end));

Réponses (1)

Walter Roberson
Walter Roberson le 11 Juin 2020
u = [u(N+1:end) zeros(1,N)];
  8 commentaires
Jin Hao Yen
Jin Hao Yen le 11 Juin 2020
Nonfinite endpoints or increment for colon operator in index.
Error in Untitled (line 42)
u = [u(N+1:end) zeros(1,N)];
Sorry Walter, still cannot proceed
Walter Roberson
Walter Roberson le 11 Juin 2020
You can get around some of the problem by using
xc = interp1(u(ind),x(ind),0.5,'cubic','extrap');
However, I have to wonder whether it is appropriate to be using x there and x(1) in the calculation of N. You have been constructing newx and it seems to me that your u values correspond to newx values, not to x values.
... However if you make that change, you still run into problems. At iteration 2700, the find() returns no values. All of the u values except u(1) are less than 1E-8 by that iteration.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by