Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.?

433 views (last 30 days)
Michelle Gaughan
Michelle Gaughan on 25 Feb 2020
Edited: Guillaume on 25 Feb 2020
The error 'Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.', is coming up for line 84. Can anyone see my issue? this is my line 84:
c(i+1) = c(i) - parError(c(i), m, g)/pardError(c(i), m, g))
i am writting an NR soultion, this is my NR soultion;
init_guess = 8;
c(1) = init_guess;
lim = 0.01;
rel_error = 1;
i = 1;
eVec = [1:20];
while ((c(i)-c(i+1))>0.01 & (i < 20))
c(i+1) = c(i) - parError(c(i), m, g)/pardError(c(i), m, g))
rel_error = abs((c(i+1)-c(i))/(c(i)))
numbers[c(i),parError(c(i), m, g)]
numbers2[pardError(c(i), m, g), c(i+1)-c(i)];
eVec(i)=rel_error;
i = i+1;
% Creates strings to be printed
str1=['At c = ', num2str(numbers2(1)), ', the error = ', num2str(numbers2(2))];
str2=['The gradient is ', num2str(numbers2(1)), ', the step is ', num2str(numbers2(2))];
disp(str);
disp(str2);
end
for the parError and pardError, these are the functions;
function error = parError( c, m, g)
error = ( (40*c) / (m*g) )-( 1-exp( (-10*c)/m ) );
end
function dError = pardError( c, m, g)
dError=(40/(m*g)) -(( 10*exp( (-10*c)/m ) )/m)
end

Accepted Answer

Guillaume
Guillaume on 25 Feb 2020
Edited: Guillaume on 25 Feb 2020
As per the error message: check for mismatched delimiters
c(i+1) = c(i) - parError(c(i), m, g)/pardError(c(i), m, g))
I count one more ) than there are (.
---
Notes:
  • I would recommend you don't use error as a variable name. It's already the name of an important matlab function.
  • These lines:
numbers[c(i),parError(c(i), m, g)]
numbers2[pardError(c(i), m, g), c(i+1)-c(i)];
are not valid syntax either.
  • I would replace:
str1=['At c = ', num2str(numbers2(1)), ', the error = ', num2str(numbers2(2))];
str2=['The gradient is ', num2str(numbers2(1)), ', the step is ', num2str(numbers2(2))];
by
str1 = sprintf('At c = %g, the error = %g', numbers2);
str2 = sprintf('The gradient is %g, the step is %g', numbers2)
  • it is puzzling that numbers2 used in both str1 and str2 contains c and the error and the gradient and the step.
  3 Comments
Guillaume
Guillaume on 25 Feb 2020
I'm not sure what the two lines are meant to do. Perhaps you're trying to assign a 2 element vector to each variable in which case you're missing a =
numbers = [c(i),parError(c(i), m, g)];
numbers2 = [pardError(c(i), m, g), c(i+1)-c(i)];
maybe?

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by