Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic variables, and function body must be sym expression.
9 views (last 30 days)
Show older comments
Salceda Fernández Barredo del Amo
on 30 Dec 2021
Commented: Salceda Fernández Barredo del Amo
on 1 Jan 2022
Hi all,
I am about to cut my veins because i have been for three days with this code and I can't fix the error. I have taken a look to the same error for other codes, but I can't see it in my code. Could someone tell me what is wrong? Below I paste the code. Thanks in advance!!
function c= regulafalsi (f, tol, ite, a, b)
% f= equation, tol=maximum error, ite = number of maximum iterations, a= right extreme of interval, b= left extreme of interval
syms x
syms f
syms a
syms b
syms c
f= @(x) exp(-x)-log(x);
f(a)= exp(-a)-log(a); %I have written these to see if I fixed the error, but not succeed
f(b)= exp(-b)-log(b);
f(c)= exp(-c)-log(c);
c = (a*f(b)-b*f(a))/(f(b)-f(a));
prod_ab=f(a) * f(b);
prod_ac=f(a) * f(c);
prod_bc=f(b) * f(c);
if prod_ab > 0
fprintf('El intervalo elegido no garantiza que contenga una solución.\n');
end
contador = 0;
while abs(b-a)>tol || contador<ite
if prod_ac<0
b = c;
elseif prod_bc<0
a = c;
elseif abs(f(c))<tol || contador<ite
fprintf('\La raíz es %f\n\n y el número de iteraciones es %f\n', c, contador)
contador = contador + 1;
end
%error messages received are:
%Error using sym/subsindex (line 953)
%Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic
%variables, and function body must be sym expression.
%Error in regulafalsi (line 9)
%f(a)= exp(-a)-log(a);
0 Comments
Accepted Answer
Voss
on 30 Dec 2021
f is a function (handle). If you want the value of f at a, you don't have to evaluate and store it like that. In fact, you cannot store something as f(a); this is the reason for the error. If you want to use the value of f at a, just say f(a) - no need to store it (but you can store it as some other variable, say, f_a = f(a), if you want to).
(Also, notice the Symbolic Math Toolbox is not used in the code below.)
f= @(x) exp(-x)-log(x);
a = 2;
b = 3;
c = (a*f(b)-b*f(a))/(f(b)-f(a));
prod_ab=f(a) * f(b);
prod_ac=f(a) * f(c);
prod_bc=f(b) * f(c);
disp(c)
disp(prod_ab)
disp(prod_ac)
disp(prod_bc)
8 Comments
Voss
on 31 Dec 2021
Notice the difference in how f was defined initially:
f= @(x) exp(-x)-log(x);
and how it is now:
exp(-x)-log(x)
You have to put the @(x) so that MATLAB knows it's a function, essentially. So you would call your function like this:
regulafalsi2(@(x) exp(-x)-log(x), 1, 1.5, 10, 0.00001)
But there is a second problem, which Walter mentioned at the start and which has not been corrected:
"In your code, where do you update prod_ab, prod_ac, prod_bc inside the while loop ?"
When you change the values of a and/or b you need to update all relevant variables that depend on those values, so not only c, which you have done, but also prod_ac and prod_bc, because if you don't update those, the code inside the while loop uses the old values which were based on the old a, b and c. (Also, notice that prod_ab is not used inside the while loop, but maybe it should be?)
More Answers (0)
See Also
Categories
Find more on Calculus 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!