What does the error Attempted to access k(3e-10); index must be a positive integer or logical. mean?

1 visualizzazione (ultimi 30 giorni)
Here is my code :-
M = 20; %number of iterations
a= 2*(10^(-10));
b= 1*(10^(-10));
U0=9;
kstart = -pi/(a+b); %Starting point
kstop = pi/(a+b); %Stop point
delta_k = (kstop - kstart)/M; %Discretization step
for n=0:1:M %begin cycle
k = kstart + n*delta_k;
k1(n+1) = k;
fun = @(x) (((1-2*x)./(2*sqrt(x.*(1-x)))).*sin(pi*sqrt(x))).*sinh(pi*sqrt(1-x)) + cos(pi*sqrt(x)).*cosh(pi*sqrt(1-x)) - cos(k(a+b));
x0 = 0.3; % Initial estimate to the solution
Sol = fzero(fun,x0); %Solve a generic equation Sin(2x) = Cos(k) numerically
Sol1(n+1) = Sol;
Sola = fzero(fun,1.0); %Solve a generic equation Sin(2x) = Cos(k) numerically
Sol2(n+1) = Sola;
Solb = fzero(fun,2.0); %Solve a generic equation Sin(2x) = Cos(k) numerically
Sol3(n+1) = Solb;
E = Sol*U0;
E1(n+1) = E;
Ea = Sola*U0;
E2(n+1) = Ea;
Eb = Sol*U0;
E3(n+1) = Eb;
formatSpec = 'Counter i = %1.0f; Wavenumber k = %1.2f; Solution_1 = %1.5f\n; Solution_2 = %1.5f\n; Solution_3 = %1.5f\n; Energy_1= %1.5f\n; Energy_2= %1.5f\n; Energy_3= %1.5f\n ';
fprintf(formatSpec,n,k,Sol,Sola,Solb,E1,E2,E3) %Print the solution
end
figure
plot(k1,E1,'g') %Plot the graph using the vectors of numerical values as above
hold on
plot(k1,E2,'b')
hold on
plot(k1,E3,'r')
axis([-1.1*(10^10) 1.1*(10^10) 0 5])
xlabel('wavenumber')
ylabel('Energy eV')
Error using fzero (line 289)
FZERO cannot continue because user supplied function_handle ==>
@(x)(((1-2*x)./(2*sqrt(x.*(1-x)))).*sin(pi*sqrt(x))).*sinh(pi*sqrt(1-x))+cos(pi*sqrt(x)).*cosh(pi*sqrt(1-x))-cos(k(a+b))
failed with the error below.
Attempted to access k(3e-10); index must be a positive integer or logical.
  3 Commenti

Accedi per commentare.

Risposte (1)

Christoph F.
Christoph F. il 19 Ott 2017
k(a+b)
means to MatLab "the (a+b)th element of vector k". However, a+b isn't integer.
Maybe the expression
cos(k(a+b))
is missing an operator somewhere?

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!

Translated by