"Subscript indices must either be real positive integers or logicals"
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I'm trying to write a script for the Linear Finite-Difference formula using my textbook's pseudo code (Numerical Analysis, Burden, 10E). Here is what I have so far and my issue. The problem is y'' = -(x+1)*y' + 2*y + (1-x^2)*exp(-x), 0<=x<=1, y(0) = -1, y(1) = 0, h=.1. When I get down to the line that has a(1) = 2 + (h^2)*q(x), it gives me this error "Subscript indices must either be real positive integers or logicals." I'm confused because my index is 1 which is a real positive integer. Any help would be appreciated. Code so far is below.
function [x,w] = linear_fin_diff(a,b,alpha,beta,h)
%Linear Finite-Difference
p = @(x) -x+1;
q = 2;
r = @(x) (1-x^2)*exp(-x);
N = ((b-a)/h)-1;
x = a + h;
a(1) = 2 + (h^2)*q(x);
0 comentarios
Respuestas (1)
Star Strider
el 2 de Abr. de 2019
Your index may be postive, however since ‘q’ is not a function, MATLAB interprets the ‘q(x)’ in this assignment:
a(1) = 2 + (h^2)*q(x);
as a subscript reference to ‘q’. This will also throw an error if ‘x’ here is anything other than 1.
2 comentarios
Star Strider
el 2 de Abr. de 2019
When you subscript your variables, you ‘grow’ them as vectors, so assigning a vector to a scalar result will throw that error. I’m not sure what you’re doing, although if you remove your subscripts, the code does what you want it to. You can always keep track of the variables in a separate matrix (‘rec’ here) if you want.
Try this:
b = 2; % Create Constant
a = 1; % Create Constant
h = 0.1;
N = ((b-a)/h)-1;
p = @(x) -x+1;
q = 2;
r = @(x) (1-x^2)*exp(-x);
rec = zeros(N-1, 5);
for i = 2:N-1
x = a + i*h;
a = 2 + h^2 * q;
b = -1 + (h/2)*p(x);
c = -1 - (h/2)*p(x);
d = -(h^2)*r(x);
rec(i,:) = [x a b c d];
end
Ver también
Categorías
Más información sobre Matrices and Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!