Forward differencing, error when plotting u(x_i)!

조회 수: 1 (최근 30일)
Andrew Sanderson
Andrew Sanderson 2011년 10월 23일
Guys,
I know this must be a reallllly trivial error, but I seriously cannot see what is going on. I am purely plotting 100 elements against another 100 elements and I am getting the following error:
??? Subscript indices must either be real positive integers or logicals.
Error in ==> Test at 22 plot(x,u);
The code I am using is below (would really appreciate some help in this because it's driving me crazy; usually i am good at debugging!):
function Test(n,f,length_check,plot)
% Create grid:
x_min = 1;
x_max = 11;
dx = (x_max - x_min)/(n-1);
x(1) = x_min;
for i = 1:(n-1);
x(i+1) = x(i) + dx;
end
x(n) = [];
% Compute u(x):
i = 1:(n-1);
u(i)= f(x(i));
if (length(x)==length(u));
'Vector lengths agree'
end
%figure;
length(x)
length(u)
plot(x,u);
axis([0,12,-0.1,0.4]);
xlabel('x'); ylabel('u');
grid on;
CHEERS FOR THE HELP GUYS!
Andrew.
  댓글 수: 2
the cyclist
the cyclist 2011년 10월 23일
It would be helpful if you used the "Code" button to format your code into a more readable form.
the cyclist
the cyclist 2011년 10월 23일
Also, it looks like you haven't defined "n" or the function "f" for us, so I don't think we can run your code to look for errors.

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

채택된 답변

Jan
Jan 2011년 10월 23일
??? Subscript indices must either be real positive integers or logicals.
Error in ==> Test at 22 plot(x,u);
The you have defined a variable called "plot". You can check this by using the debugger:
dbstop if error
Then start your program until it stops at the error. Now user this to check the type of "plot":
which plot
whos plot
Solution: Do not use names of builtin functions for variables.
  댓글 수: 2
Andrew Sanderson
Andrew Sanderson 2011년 10월 23일
OH! Of course... Thanks so much for this help. And for the other guys; sorry for the unreadable code, this was my first ever post on the matlab forums :)
Jan
Jan 2011년 10월 23일
You do not have to apologize. Simple edit the original question, mark the code and press the "{} code" button. I've done this for you this time.

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

추가 답변 (1개)

Andrew Sanderson
Andrew Sanderson 2011년 10월 23일
function Development_Task_1(n,f,length_check,plot_check)
% Create grid:
x_min = 1;
x_max = 11;
dx = (x_max - x_min)/(n-1);
x(1) = x_min;
for i = 1:(n-1);
x(i+1) = x(i) + dx;
end
x(n) = [];
% Compute u(x):
i = 1:(n-1);
u(i)= f(x(i));
if (length(x)==length(u));
'Vector lengths agree'
end
%figure;
length(x)
length(u)
plot(x,u);
axis([0,12,-0.1,0.4]);
xlabel('x'); ylabel('u');
grid on;
% We must compute:
%
% du u(i+1) - u(i)
% -- = -------------
% dx dx
%
% Using the method of forward differencing;
i = 2 : (n-1);
dudx(i) = (u(i) - u(i-1)) / dx;
dudx(n-1) = [];
figure;
plot(x,dudx);
if (plot_check == 1)
if (length_check == 1)
fig_ = figure;
lengths_ = [length(x), length(u), length(dudx)];
names_ = {'x','u(x)','u''(x)'};
uitable(fig_, 'Data', lengths_, 'ColumnName', names_);
end
end
Sorry after changing the input to plot_check I am still getting the same error.
The inputs I have been using are;
n = 100; f = @(x) exp(-x);
Thanks,
Andrew.
  댓글 수: 1
Andrew Sanderson
Andrew Sanderson 2011년 10월 23일
EDIT: Sorry, I am having an absolute nightmare with matlab today. The above answer did work, apologies for the stupidity and cheers for all the help!

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

카테고리

Help CenterFile Exchange에서 Computational Fluid Dynamics (CFD)에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by