"Subscript indices must either be real positive integers or logicals"
6 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (1)
Star Strider
il 2 Apr 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 Commenti
Star Strider
il 2 Apr 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
Vedere anche
Categorie
Scopri di più su Matrices and Arrays in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!