필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
% 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));

답변 (1개)

Walter Roberson
Walter Roberson 2020년 6월 11일
u = [u(N+1:end) zeros(1,N)];
  댓글 수: 8
Jin Hao Yen
Jin Hao Yen 2020년 6월 11일
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 2020년 6월 11일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by