Why am I getting error: subscript indices must either be real positive integers or logicals? (Boundary Value Problem)
Show older comments
The question I am trying to solve is: put statements in your script file ma12tta a2.m which computes the approximate solution to your given problem for N = 2r, r = 4, 5, · · · , 10 and for each value of r you should neatly output N and max {|u(xi) − Ui| : i = 0, · · · ,N}. To compute the last quantity you thus need to evaluate the exact solution at all the mesh points and you need to compare with the approximate solution which is one of the outputs of ma12tta fdm.m. You can use the Matlab built-in function norm in the computation. If the function is working properly then you should observe that the error is eventually decreasing by a factor of about 4 when N is doubled.
_____________________________________________________________________________________________ The error I am recieving when running the main code:
??? Subscript indices must either be real positive integers or logicals.
Error in ==> ma12tta_q at 3 q(x)=1+0.01*9*x.*(8-x);
Error in ==> ma12tta_fdm at 10 A=spdiags([-o-0.5*h*p(x), 2*o+h*h*q(x), -o+0.5*h*p(x)], -1:1, N1, N1);
Error in ==> ma12tta_a2 at 24 [xx,U]=ma12tta_fdm(p, q, r, N, a, g1, b, g2);
___________________________________________________________________________
Main body of code I am compiling on called ma12tta_a2:
a=0;
b=8;
x=linspace(a,b);
u=ma12tta_exactu(x);
g1=ma12tta_exactu(a);
g2=ma12tta_exactu(b);
plot(x,ma12tta_exactu(x))
xlabel('x', 'Fontsize', 11)
ylabel('u(x)', 'Fontsize', 11)
title('Solution to f(x)');
print -deps ma12tta_exactu.eps
p=@ma12tta_p;
q=@ma12tta_q;
r=@ma12tta_r;
%Task 6
fprintf('%5s %12s %10s\n', 'N', 'error', 'ratio');
for k=4:10;
N=2^k;
[xx,U]=ma12tta_fdm(p, q, r, N, a, g1, b, g2);
er(k)=norm((ma12tta_exactu(xx)-U),inf);
if k>1
rat=er(k-1)/er(k);
fprintf('%5d %12.3e %10f\n', N, er(k), rat);
else
fprintf('%5d %12.3e/n', N, er(k))
end
end
_____________________________________________________________________________________________
My ma12tta_p:
function p=ma12tta_p(x)
p=3*0.01;
end
My ma12tta_q:
function q=ma12tta_q(x)
q(x)=1+0.01*9*x.*(8-x);
end
My ma12tta_r:
function r=ma12tta_r(x)
[u,u1,u2]=ma12tta_exactu(x);
r= u2-(ma12tta_p(x).*u1 + ma12tta_q(x).*u);
end
My ma12tta_fdm
function [xx,U]=ma12tta_fdm(p, q, r, N, a, g1, b, g2)
xx=linspace(a, b, N+1);
xx=xx(:);
h=(b-a)/N;
x=xx(2:N);O
% set-up the tri-diagonal matrix A
N1=N-1;
o=ones(N1, 1);
A=spdiags([-o-0.5*h*p(x), 2*o+h*h*q(x), -o+0.5*h*p(x)], -1:1, N1, N1);
% set-up the rhs vector c
c=-r(x).*h*h;
c(1)=c(1)+(1+0.5*h*p(a))*g1;
c(N1)=c(N1)+(1-0.5*h*p(b))*g2;
% get the fd solution U and save the error
U=[g1; zeros(N1, 1); g2];
U(2:N)=A\c;
end
My ma12tta_exactu:
function [u,u1,u2]=ma12tta_exactu(x)
u=(-1).*x/8 + x.*(8-x).*(cos(2*pi*x/8)+(3/8)*cos(5*x));
u1=x.*((15*sin(5*x))/8+(pi*sin((pi*x)/4))/4).*(x-8)-((3*cos(5*x))/8+cos((pi*x)/4)).*(x-8)-x.*((3*cos(5*x))/8+cos((pi*x)/4))-1/8;
u2=2.*x.*((15*sin(5*x))/8+(pi*sin((pi*x)/4))/4)-2.*cos((pi*x)/4)-(3*cos(5*x))/4+2.*((15*sin(5*x))/8+(pi*sin((pi*x)/4))/4).*(x - 8)+x.*((75*cos(5*x))/8+(pi^2*cos((pi*x)/4))/16).*(x-8);
end
Answers (1)
dpb
on 31 Mar 2014
a=0;
b=8;
x=linspace(a,b);
...
Ergo,
q(x)=1+0.01*9*x.*(8-x);
Is going to be
q(0)=1+0.01*9*x.*(8-x);
at the first call. You can't intermix using x as the coordinates with the indexing of an array that MUST be 1:N
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!