Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.

Why does MatLab keep telling me this about my program? I need some help please.
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To
construct matrices, use brackets instead of parentheses.

 Accepted Answer

As has been said, this line has invalid MATLAB code:
Ta = Tinf+ (Tb-Tinf)cosh(m(L-xa))/cosh(m*L);
You need to learn about operators in MATLB. If you don't use them, then your code won't work. MATLAB does not just assume multiplication when no * operator is present. In fact, it assume that you are trying to do something else, and then it tiries to make sense of what you wrote.
What does that mean? You apparently want to MULTIPLY the sub-terms m and (L-xa). I claim that, because m appears to be a constant. Does MATLAB know that m(L-xa) means multiplication? OF COURSE NOT. MATLAB cannot read your mind. Instead, it looks at that expression, and thinks, hmm. m is a variable. I guess they want me to index the variable m, thus finding element with index (L-xa). After all, you did say that you got an error telling you that "Array indices must be positive integers or logical values."
So if you know that you want to multiply, then use * or .* as appropriate! Don't ask MATLAB to read your mind.
Similarly, what does this mean?
(Tb-Tinf)cosh(m(L-xa))
Did you want to multiply the terms (Tb-Tinf) and cosh(m(L-xa))? Did you use * or .* there? Should MATLAB know what you want? Should it read your mind? NO. (Well, some think it should, but computers are not that smart, not yet. I'm not even sure I want them to be that smart.)
Look at the error message. What does it say? It points at the point between the two parens in that expression. (If you fix this, so it will get past that point, it would next start complaining about m, but it fails before it ever gets there, as youtr code stands now.)
So how should you modify that line? Use operators when you want to multiply things! After all, you know that you need to use / when you want to divide, + when you want to add, - when you want to subtract. Is multiplication a second class citizen among operators?
Ta = Tinf+ (Tb-Tinf)*cosh(m*(L-xa))/cosh(m*L);
Finally, make sure you know the difference between * and .*, ^ and .^, and / and ./, if there are vectors and arrays involved, as otherwise your next question willl again be why does my code not work.

8 Comments

Hi can someone help me too please :(
I have the same problem
The system said line 8 which is the code that I highlighted and column 45
Hope someone can help me
In the future when you post a piece of code please post it as text in addition to or instead of as a picture. It's hard to run a picture or copy and paste it into the MATLAB Editor to help with analysis.
disp(['Root X = ' num2str(X(end))], 'Fx = ' num2str(f(X(end))) ', # Iterations ');
Did you intend to end the concatenation immediately after the first num2str call? If you move the closing square bracket to just inside the closing parentheses of the disp call Code Analyzer no longer flags anything about that line and when I give sample values for X and f (X = pi, f = @sin) that code runs as I expected as well.
Thank you so much for replying. Actually I am really new to this Matlab. Just today I installed it. I was assigned to create Steffensen's Method in any software. So I chose Matlab because there are tutorials on youtube. So I tried to follow the steps as in the tutorial on youtube as a practice but I can't run it. I don't really know where the mistake is. This code is like the tutorial on youtube and its link:
%% Steffensen's method
% a root-finding technique similar to Newton's method, named after Johan
% Frederik Steffensen. Steffensen's method also achieves quadratic
% convergence, but without using derivatives as Newton's method does
clc; clear all; close all
% Presenting Results
disp(['Root X = ' num2str(X(end)) ' ,Fx = ' num2str(f(X(end))) '#, Iterations ';
Iterations = [1:ii+1]';
Root = X';
Fx = [f(X)]';
ERROR = [10 Err]';
RESULTS = table(Iterations, Root, Fx, ERROR)
f = @(x) x.^3 - 8; % Equation we interest to solve
p = rand(1,1); % Initial values [p0]
N = 1e6; % Max. number of iterations
tol = 1e-6; % Tollerance
[X,Err,ii] = Steffensen(f,p,tol,N); % Calling Steffensen's Function
%----------------------------
% Main Function (Steffensen's Method)
function [p, Err, i] = Steffensen(f,p,tol,N)
Unsupported use of the '=' operator. To compare values for equality, use '=='. To specify name-value arguments, check that name is a valid identifier with no surrounding quotes.
format compact
format long
for i=1:N
p(i+1) = p(i) - f(p(i))^2 / (f(p(i) + f(p(i))) - f(p(i)));
Err(i) = abs(p(i+1) - p(i));
if Err(i) < tol
break
elseif isnan(Err(i)== 1)
p = p(1:i);
Err = [Err(1:i-1) 0];
break
end
end
if i == N && abs(p(i+1) - p(i))>tol
disp(['failed to converge in ' num2str(N) ' iterations.'])
end
end
disp(['Root X = ' num2str(X(end)) ' ,Fx = ' num2str(f(X(end))) '#, Iterations ';
% 12 3 4 32 3 4 5 432
The digit is the number of unmatched open brackets "after" the character above them. You can see from this that there are two missing close brackets. One of them needs to be a ']' character, and the other needs to be after the ']' character.
disp(['Root X = ' num2str(X(end)) ' ,Fx = ' num2str(f(X(end))) '#, Iterations ']);
% **
* shows the addition of a missing character. Did I mark the star correctly?
Or can I change like this?
A = 'Root X = ';
B = num2str(X(end));
C = ' Fx = ';
D = num2str(f(X(end)));
E = '# Iterations ';
F = [A, B, C, D, E];
disp(F)
Since you're new to MATLAB, I suggest you start with the free MATLAB Onramp tutorial (https://www.mathworks.com/support/learn-with-matlab-tutorials.html) to quickly learn the essentials of MATLAB.

Sign in to comment.

More Answers (1)

MATLAB has absolutely no implicit multiplication. (A)B is never valid in MATLAB.
A(B) in MATLAB would always indicate either indexing or function call and never multiplication. Your m(L-xa) is either indexing m at location L-xa or else is calling function m. It does not mean to multiply m by L-xa
In every case in MATLAB if you want to multiply two values, you need to use the .* operator for element-by-element multiplication, or the * operator for algebraic matrix multiplication (inner product)

5 Comments

So what exactly am I suppose to change about this particular equation?
Find every place that you intend for there to be multiplication. If there is presently no operation at that point in the code, insert .* there. If there is * at that point in the code, change it to .*
For example (Tb-Tinf).*cosh
I tried doing that then it said
Array indices must be positive integers or logical values.
This is the full program for context
% Heat transfer through fin using finite volume method clear all; clc;
h = 50.0;
k= 100.0;
Tinf = 373.15; % 100 C is converted to Kelvin
Tb = 473.15; % base temperature in K
Tinf0 = 473.15; % 200 C is converted to Kelvin
hinf = 10E20;
L = 1.0; % fin length in m
d = 0.08; % fin diameter in m
m = 4.0*h/(k*d);
%N = 40; % no. of cellslices
for N = [160]
dx = L/N; % cell size
% By taking base of fin as node 1
% Dirichelet boundary condition
aw(1) = 0;
ae(1) = 0;
sp(1) = 0;
su(1) = Tb; % 200 C is converted to Kelvin
ap(1) = 1;
% node in first cell- node 2
aw(2) = 2/dx;
ae(2) = 1/dx;
sp(2) = -m*m*dx;
su(2) = m*m*Tinf*dx;
ap(2) = aw(2)+ae(2)-sp(2);
% for internal nodes
for i =3 : N
aw(i) = 1/dx;
ae(i) = 1/dx;
sp(i) = -m*m*dx;
su(i) = m*m*Tinf*dx;
ap(i) = aw(i)+ae(i)-sp(i);
end
% For Node at fin tip
aw(N+1) = 1/dx;
ae(N+1) = 0;
sp(N+1) = -m*m*dx;
su(N+1) = m*m*Tinf*dx;
ap(N+1) = aw(N+1)+ae(N+1)-sp(N+1);
% rearrange coefficient for TDMA
for i=1:N+1
a(i) = ap(i);
b(i) = ae(i);
c(i) = aw(i);
d(i) = su(i);
end
% solving using TDMA
P(1) = b(1)/a(1);
Q(1) = d(1)/a(1);
for i = 2: N+1
P(i) = b(i)/( a(i)-c(i)*P(i-1) );
Q(i) = ( d(i)+c(i)*Q(i-1) )/( a(i)-c(i)*P(i-1));
end
T(N+1) = Q(N+1);
for i = N:-1:1
T(i) = P(i)*T(i+1)+Q(i);
end
x(1) = 0;
x(2) = dx/2;
x(N+1) = L;
for i=3:N
x(i) = x(2)+(i-2)*dx;
end
end % end N loop
% analytical solution
xa=0:L/40:L;
Ta = Tinf+ (Tb-Tinf)cosh(m(L-xa))/cosh(m*L); plot(xa,Ta,'ok','Linewidth',1)
set(gca,'fontsize',16);
ylabel('Temperature(K)','fontsize',16);
xlabel('X (m)','fontsize',16);
hold on;
plot(x,T,'r','Linewidth',2)
set(gca,'fontsize',16);
ylabel('Temperature(K)','fontsize',16);
xlabel('X (m)','fontsize',16);
legend('Numerical','Analytial')
Ta = Tinf+ (Tb-Tinf)cosh(m(L-xa))/cosh(m*L); plot(xa,Ta,'ok','Linewidth',1)
As you would have followed my instructions to put .* everywhere that multiplication was intended, then we have ot ask what your intention is for the expression (Tb-Tinf)cosh(m(L-xa)) if it is not multiplication between (Tb-Tinf) and cosh(m(L-xa)) ? And what is your intention for the expression m(L-xa) if you did not intend multiplication between m and (L-xa) ?

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!