I have a code but it keep saying "invalid expression. when calling a function or indexing a variable, use parentheses. otherwise check for mismatched delimiters.
Show older comments
F=[];
A=[];
for f=0:200:1800
F =[F,f]; %in HZ
f0 = 500;% in Hz
f1= 1500;% in Hz
N = 20;
a = 0:5:25
a= abs((a*sin[pi(f-f0)*N]))/ (sin[pi(f-f0)]))+ abs((a*sin[pi(f-f1)*N]))/ (sin[pi(f-f1)]));
A= [A, a];
end
plot (F, A)
12 Comments
Roolientha Denaud
on 15 Nov 2018
Abhijeet Gadge
on 17 Feb 2021
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
Leila Nait-Kaci
on 15 May 2022
Hello, im new to matlab and need some assistance: i keep getting this error even though i do not have any mismatch with my parentheses, Y= sin^2((2/3)*pi)+(1/3)*cos^2((2/3)*pi) If anyone could help me within the next two days (i have a test coming up) i would be very thankful.
Rik
on 15 May 2022
sin^2 is a common mathematical notation, but not valid Matlab syntax. In Matlab you need to write it like this:
sin(x)^2 % for matrix multiplication (usually only for scalar inputs)
sin(x).^2 % for element-wise power (use for array input)
John D'Errico
on 15 May 2022
It is a common mathematical way of writing the power of a trig function, as for example:
sin^2(x)
HOWEVER, that does not make it valid MATLAB syntax. If you want to square the sin(x), then write:
sin(x)^2
Leila Nait-Kaci
on 15 May 2022
It solved the problem Thank you very much!
Adriana
on 29 Nov 2022
Can someone help me please? I don't know why it isn't working.
clear all; close all; clf; clc;
g = 9.81; %m/s2 Valor de Aceleración Gravitacional
p = 960; %Densidad del Fluido en kg/m^3;
V = 5.08e-10; %Volumen de la Partícula en m^3
m = 1.3e-6; %Masa de la Partícula en kg
r = 4.95e-4; %Radio de la Partícula en m
po = 2559.055; %Densidad de la Partícula en kg/m^3
n = 0.046; %Viscosidad en kg/m/s
b = (-p*g*V)+(m*g)/(2*r^2*(po-p)*g)/9*n; %Constante de Amortiguamiento
v = (2*r^2)*((po-p)*g/9*n);
w = 108.28; %Velocidad Angular en rad/s
%no = 20; %Número de partículas
dt = 0.01;
to = 0;
tf = 10;
t= (to:dt:tf)';
T = numel(t);
vox = 0;
vx = zeros(length(t),no); %Regresar a poner no
vx(1,1) = vox;
aox = 0;
ax = zeros(length(t),no); %Regresar a poner no
ax(1,1) = aox;
int1=-0.055;
int2=0.055;
xo = int1+(int2-int1) *rand(1,no); %Aqui
x = zeros(length(t),no); %Aqui
x(1,1) = xo;
voy = 0;
vy = zeros(length(t),no); %Aqui
vy(1,1) = voy;
int3=0.14;
int4=0;
yo = int3+(int4-int3) *rand(1,no); %Aqui
y = zeros(length(t),no); %Aqui
y(1,1) = yo;
for i=2:1:T
vx(i-1,1) = ((w^2*x(i-1,1)(m-p.*V)) - (b.*vx(i-1,1)) + ((m.*vx(i-1,1))/dt))(dt/m);
x(i) = x(i-1) + vx(i-1,1)*dt;
vy(i-1,1) = -g + (((po*g*V)-((A*g*V).*y(i-1,1)))/m) - ((6*pi*r*n*v(i-1,1))/m);
y(i) = y(i-1) + vy(i-1,1)*dt;
figure(1)
subplot(1,2,1)
plot(t(i,1),vx(i,1),'r-o')
grid on;
xlabel('Tiempo [segundos]');
ylabel('Posición Vertical y [metros]');
hold on
subplot(1,2,2)
plot(t(i,1),x(i,1),'r-o')
grid on;
xlabel('Tiempo [segundos]');
ylabel('Posición Vertical y [metros]');
hold on
end
Can someone help me?
Walter Roberson
on 29 Nov 2022
vx(i-1,1) = ((w^2*x(i-1,1)(m-p.*V)) - (b.*vx(i-1,1)) + ((m.*vx(i-1,1))/dt))(dt/m);
You have x(i-1,1)(m-p.*V) . That code asks to take x, index it at (i-1,1), and then index the result at (m-p.*V) . However, MATLAB does not permit () indexing of calculated values. The calculated value would be a scalar, and it seems unlikely that m-p.*V will always happen to be exactly 1
Likewise you have ((m.*vx(i-1,1))/dt))(dt/m) which requests that the result of calculating ((m.*vx(i-1,1))/dt)) be indexed at dt/m which seems unlikely to be an exact integer.
MATLAB has absolutely no implicit multiplication, not anywhere that I have found. If you want to multiply, you must use the .* or * operators
clear all; close all; clf; clc;
g = 9.81; %m/s2 Valor de Aceleración Gravitacional
p = 960; %Densidad del Fluido en kg/m^3;
V = 5.08e-10; %Volumen de la Partícula en m^3
m = 1.3e-6; %Masa de la Partícula en kg
r = 4.95e-4; %Radio de la Partícula en m
po = 2559.055; %Densidad de la Partícula en kg/m^3
n = 0.046; %Viscosidad en kg/m/s
b = (-p*g*V)+(m*g)/(2*r^2*(po-p)*g)/9*n; %Constante de Amortiguamiento
v = (2*r^2)*((po-p)*g/9*n);
w = 108.28; %Velocidad Angular en rad/s
%no = 20; %Número de partículas
dt = 0.01;
to = 0;
tf = 10;
t= (to:dt:tf)';
T = numel(t);
vox = 0;
vx = zeros(length(t),1); %Regresar a poner no
vx(1,1) = vox;
aox = 0;
ax = zeros(length(t),1); %Regresar a poner no
ax(1,1) = aox;
int1=-0.055;
int2=0.055;
xo = int1+(int2-int1) *rand(1,1); %Aqui
x = zeros(length(t),1); %Aqui
x(1,1) = xo;
voy = 0;
vy = zeros(length(t),1); %Aqui
vy(1,1) = voy;
int3=0.14;
int4=0;
yo = int3+(int4-int3) *rand(1,1); %Aqui
y = zeros(length(t),1); %Aqui
y(1,1) = yo;
for i=2:1:T
vx(i-1,1) = ((w^2*x(i-1,1)*(m-p.*V)) - (b.*vx(i-1,1)) + ((m.*vx(i-1,1))/dt))*(dt/m);
x(i) = x(i-1) + vx(i-1,1)*dt;
figure(1)
subplot(1,2,1)
plot(t(i,1),vx(i,1),'r-o')
grid on;
xlabel('Tiempo [segundos]');
ylabel('Posición Vertical y [metros]');
hold on
subplot(1,2,2)
plot(t(i,1),x(i,1),'r-o')
grid on;
xlabel('Tiempo [segundos]');
ylabel('Posición Vertical y [metros]');
hold on
en
We have these two graphs but we need to have an oscilation type of graph. Can you help me?
Walter Roberson
on 29 Nov 2022
I think you should open your own Question about this.
Accepted Answer
More Answers (8)
You are using square brackets for the sin function, instead of parentheses. Also, you were missing a lot of * signs. You should check if the four constituents are indeed what you need in the line below.
a= abs((a*sin(pi*(f-f0)*N))) / sin(pi*(f-f0)) + abs((a*sin(pi*(f-f1)*N))) / sin(pi*(f-f1));
You have another problem here: you are trying to plot A against F, but A will have 6 values for each value of F, because of that first assignment of a.
You could speed this up by first fixing the dynamic growth of these vectors by pre-allocating them, and it should also be possible to remove the loop entirely once you have code that does what you intend to do.
2 Comments
Roolientha Denaud
on 15 Nov 2018
Rik
on 15 Nov 2018
Have you copied the line exactly as I posted it? Because for me it doesn't error at that line. I do have an error when plotting, because there you haven't explained how you want to extend F to have enough values. The code below assumes you want to repeat the values of F.
F=0:200:1800;%in HZ
f0 = 500;% in Hz
f1= 1500;% in Hz
N = 20;
a = 0:5:25;
A=zeros(numel(a),numel(F));
for n=1:numel(F)
f=F(n);
a= abs((a*sin(pi*(f-f0)*N))) / sin(pi*(f-f0)) + abs((a*sin(pi*(f-f1)*N))) / sin(pi*(f-f1));
A(:,n)=a;
end
plot (F, A)
Steven Lord
on 15 Nov 2018
If you're editing this code in an editor other than the MATLAB Editor, I advise you to copy and paste it into the MATLAB Editor. If you have Code Analyzer enabled in the Preferences (it is enabled by default) you should see a red square in the upper-right corner of the Editor window and several colored lines (three orange, one red) below it. Orange lines indicate places where your code should work, but Code Analyzer has a suggestion that may improve its robustness and/or performance. Red lines indicate lines of code where its analysis indicates errors that will prevent your code from running at all. Clicking on the red line brings the cursor to the line in question, where the suspected error should be underlined.
If you hover over the underlined suspected error (or warning), you will receive some additional information that may help you understand the suggestion or error. In this case, once I fixed the sin[...] sections there is still an error. Counting, you have mismatched parentheses. Here I've written numbers below the locations of the parentheses in your code. Everywhere I see a ( I add 1 to the number, and everywhere I see a ) I subtract 1.
a= abs((a*sin(pi(f-f0)*N)))/ (sin(pi(f-f0))))+ abs((a*sin(pi(f-f1)*N)))/ (sin(pi(f-f1))));
12 3 4 3 210 1 2 3 210*
The * indicates -1, where I saw more closing parentheses than opening parentheses. Delete that ) and continue counting on that line of code.
1 Comment
Merve Özkanat
on 3 Oct 2022
Hi I get the same error, could you help me please?
I want to create a yxoordinat vector [11x1] with values from a vector d.
d is a vector with 363 values.
d(afg)=dr; % d is a [363,1] vector with values.
ycoordinat=zeros(11,1);
for i=56:66
for s=1:11
ycoordinat[s,1]=d((i-1)*3+1) % d((i-1)*3+1) is the value that should be put into a vector.
end
end
But it says ''Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters''.
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters
what's is the problem?
it always shows me this comment for this code
Line: 1 Column: 23
[t,c]=ode45('prob5_16',[0,6],[0.2]);
clinear=0.2+0.111*(1-exp(-t/1.11));
plot(t,c,t,clinear)gridxlabel('time')ylabel('concentration')>>title('Solution for Problem 5.16');
3 Comments
Rik
on 20 May 2020
This is not an answer, but a question.
The standard Matlab syntax is to separate function calls by comma, semicolon, or newline. You did none of that on your last line.
Merve Özkanat
on 3 Oct 2022
Hi I get the same error, could you help me please?
I want to create a yxoordinat vector [11x1] with values from a vector d.
d is a vector with 363 values.
d(afg)=dr; % d is a [363,1] vector with values.
ycoordinat=zeros(11,1);
for i=56:66
for s=1:11
ycoordinat[s,1]=d((i-1)*3+1) % d((i-1)*3+1) is the value that should be put into a vector.
end
end
But it says ''Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters''.
Rik
on 4 Oct 2022
You're using square brackets instead of parentheses.
Tahlia Jones
on 20 Mar 2021
0 votes
[2*3^12[cos140+sin140]/(5+3i)^3(2+3i);-(3-5i)3/25[cos-60sin-60]]
1 Comment
Walter Roberson
on 20 Mar 2021
MATLAB has no implied multiplication. You have to put in every multiplication operator.
Also in MATLAB, function calls have to have their parameters in () such as sin(pi/3)
The MATLAB sin and cos operations are in radians but there is sind() and cosd()
Remember to put in () around denominators that involve expressions A/B*C is (A/B)*C not A/(B*C)
ali sh
on 31 Jan 2022
0 votes
mu=mu0*(1+omega*(3-(n2)^2);
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched
delimiters.
1 Comment
Steven Lord
on 31 Jan 2022
You have three left parentheses ( but only two right parentheses ). Thus you have a mismatch in your parentheses. You need one more ) but where to put it depends on what you're trying to compute.
Jobin Geevarghese Thampi
on 17 Mar 2022
Edited: Walter Roberson
on 17 Mar 2022
functiom k=summa(n)
[row col]=size(n);
k=0;
for ii=1:row
for jj=1:col
if (ii,jj)>=5 && (ii,jj)<=18
k=k+n(ii,jj);
end
end
end
File: summa.m Line: 6 Column: 15
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check
for mismatched delimiters.
2 Comments
Jobin Geevarghese Thampi
on 17 Mar 2022
can anyone help me on this
Walter Roberson
on 17 Mar 2022
if n(ii,jj)>=5 && n(ii,jj)<=18
Kamilu Sanusi
on 2 May 2022
Edited: Walter Roberson
on 2 May 2022
Hello please I am having a similar problem, i need assistance please. What I keep getting is "Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters"
and is pointing to Line 2 column 35
Here is the code:
clear all
clc
% User Interface pop-up window
prompt=('Power Input_Bus1 [p.u.]:','Z_1 [p.u.]:','Z_12 [p.u.]:','Load_Bus2:','V2:','P.U. MVA Base:');
dlg_title='Power Flow Analysis';
defaultans =('1.06+0i','6i','0.2i','1.6+0.20i','1+0i','100');
num_lines = [ones(size(defaultans')) ones(size(defaultans'))*50];
answer=inputdlg(prompt,dlg_title,num_lines,defaultans);
% Reading all inputs and change string to numeric value
% ****************************%
v1 = str2num(answer(1)); % Read power input V1
Z_1 = str2num(answer(2)); % Read impedance input z1
Z_12 = str2num(answer(3)); % Read impedance input z12
L_B2 = str2num(answer(4)); % Read load bus input s2
v2 = str2num(answer(5)); % Read voltage 2 v2
pubase = str2num(answer(6)); % PU MVA base
% Variales declared
% Convert impedances to admittances
G1= 1/Z_1 % Self reactance
y12 = 1/Z_12
% Identify load P (real) and Q (imag) value
s2 = -1*L_B2,
p2 = real(s2);
q2 = imag(s2);
% Create the bus admittance matrix
y = [(G1+y12) -(y12);-(y12) (y12)];
% Load flow calculation using Gauss siedel
n = 1 %n y loop declaration
while n > 0.00001
v2e = (((p2-(q2)*i)/conj(v2))+y12*v1)/y12;
e2 = abs(v2e-v2);
v2 = v2e;
em = [e2];
n = max(em);
end
% s1 calculation
s1 = v1*(v1*(G1+y12)-(y12*v2)); % slack bus apparaent power
P1 = real(s1);
Q1 = imag(s1);
% Determine line flow and line losses
I12 = y12*(v1-v2);
I21 = -I12;
% Line flow
s12 = v1*conj(I12);
s21 = v2*conj(I21);
% Line losses
slose_12 = s12 + s21;
%***********************************************************************%
% Displar results
% **********************************************************************%
disp('Bus admittance matrix:')
disp(' yi1 yi2')
disp(y)
disp('V2 value=')
disp(v2)
disp('s1 value=')
disp(s1)
disp('slose_12 value=')
disp(slose_12)
4 Comments
Kamilu Sanusi
on 2 May 2022
It keeps pointing to propmt row
Walter Roberson
on 2 May 2022
In MATLAB, () can occur in a few different contexts:
- (one_expression) indicates that one_expression is to be grouped together for priority. For example, 3*(5+7) . Here, one_expression is intended to indicate that you have a single expression not separated by commas
- name(expression1, expression2, expression3, ...) might indicate that the given name is an array that is being indexed at the given locations. Each comma marks the end of a dimension
- name(expression1, expression2, expression3, ...) might indicate that the given name is the name of variable containing a function handle, and that the function is to be executed passing in the values as parameters. Each comma marks the end of a parameter.
- name(expression1, expression2, expression3, ...) might indicate that the given name is the name of a function, and that the function is to be executed passing in the values as parameters. Each comma marks the end of a parameter.
- There are some cases in which extra () will be silently ignored, such as some ways of writing for loops
- () can sometimes occur after keywords in order to give options for the keywords; see for example passing in a pool to parfor
In MATLAB, there are no cases in which you can have (expression1, expression2, expression3, ...) as an expression to create a list of any kind. For example your
defaultans =('1.06+0i','6i','0.2i','1.6+0.20i','1+0i','100');
is simply invalid in MATLAB, not a way to create a list of character vectors.
In MATLAB, to create a list of character vectors, use { } instead of ( ) . So for example,
defaultans = {'1.06+0i','6i','0.2i','1.6+0.20i','1+0i','100'};
If you were trying to create a list of numeric values, then using { } would not necessarily be wrong, but typically you would instead use [ ] to create a numeric vector. For example,
answer_values = [1.06+0i, 6i, 0.2i, 1.6+0.20i, 1+0i, 100];
Kamilu Sanusi
on 2 May 2022
@Walter Roberson, thank you so much, your really inputs really helped.
Please I am a beginner still learning from original codes written by people. Kindly, can you explain what the
num_lines = [ones(size(defaultans')) ones(size(defaultans'))*50]; and answer=inputdlg(prompt,dlg_title,num_lines,defaultans); are meant to implement? and also that specific number 50 in the num_line.
Thank you so much
Walter Roberson
on 2 May 2022
defaultans is a cell row vector. defaultans' is its transpose, and so is a cell column vector. size(defaultans') would be a vector with the number of entries in defaultans as its first value, and 1 (only one column for the transpose) as the second value. ones() of that would be an array that is all ones and has as many ones in a column as there were entries in defaultans .
Likewise the second part is similar but the *50 multiplies it by 50.
Those are together in [] so what you get is an array that has as many rows as there are entries in defaultans, and the first column is all 1 and the second column is all 50.
When that array is passed in to inputdlg() in the position it is in, it gives information about the dimensions of each input area. The leading 1 means "reserve one line of text for each input area" and the trailing 50 means "reserve 50 characters for each input area".
Why 50? Well, that appears to be an arbitrary design decision. It looked good to whoever wrote the code.
function [f]=fad(0.01,0.1,0.15,0.5);
%
% Factor de Amplificación Dinámica
%
% Por: Roberto Aguiar Falconi
% CEINCI-ESPE
% ---------------------------------------
% [f]=fad(z1,z2,z3,z4)
% ---------------------------------------
% z1: Factor de amortiguamiento 1
% z2: Factor de amortiguamiento 2
% z3: Factor de amortiguamiento 3
% z4: Factor de amortiguamiento 4
% r : Relación entre la frecuencia excitación a frecuencia natural
% f : Factor de amplificación dinámica
hold off
dr=0.02;r=0;
for i=1:150
r=r+dr;
f(i)=1.0/(sqrt((1-r^2)^2+(2*z1*r)^2));f1(i)=1.0/(sqrt((1-r^2)^2+(2*z2*r)^2));
f2(i)=1.0/(sqrt((1-r^2)^2+(2*z3*r)^2));f3(i)=1.0/(sqrt((1-r^2)^2+(2*z4*r)^2));
rr(i)=r;
end
plot (rr,f); hold on
plot (rr,f1,'--'); plot (rr,f2,':'); plot (rr,f3,'-.')
xlabel('r'); ylabel('Factor de amplificacion');
axis([0,3,0,5]);
text (2.0,4.5,'z1 ---- ','Fontname','symbol'); text (2.0,4.0,'z2 - - -','Fontname','symbol')
text (2.0,3.5,'z3 .......','Fontname','symbol'); text (2.0,3.0,'z4 .-.-.-','Fontname','symbol')
hold off
% ---fin---
5 Comments
michael
on 28 Oct 2023
>> newtonraphson
File: newtonraphson.m Line: 1 Column: 28
Invalid expression. When calling a function or indexing
a variable, use parentheses. Otherwise, check for
mismatched delimiters.
Walter Roberson
on 28 Oct 2023
function [f]=fad(0.01,0.1,0.15,0.5);
What is the function going to do with the 0.01?
Where does the function get values for z1 z2 z3 z4?
% z1: Factor de amortiguamiento 1
comments are not executable. MATLAB is not going to look at the comment and deduce that z1 is a parameter to the function.
Walter Roberson
on 28 Oct 2023
Suppose you define three functions:
function cost = fad1(3, 2)
cost = slices_of_pizza * 2.50 + pieces_of_chicken * 1.99;
end
function cost = fad2(3, 2)
cost = pieces_of_chicken * 1.99 + slices_of_pizza * 2.50;
end
function cost = fad3(3, 2)
if rand() < 0.5
cost = slices_of_pizza * 2.50 + pieces_of_chicken * 1.99;
else
cost = pieces_of_chicken * 1.99 + slices_of_pizza * 2.50;
end
end
What would be your expected output for each of the two functions?
Are you assuming that the first-mentioned variable gets assigned the first value (3) and the second-mentioned variable gets assigned the second value (2) ? If so then is "first mentioned" according to parsing or according to execution?
If it is according to parsing (fad3 mentions slices_of_pizza first even though that statement might not be executed) then suppose you have
function cost = fad3(3, 2)
if false
cost = bottles_of_2L_softdrink * 3.99 + cartons_of_500ml_ice_cream * 6.99;
elseif rand() < 0.5
cost = slices_of_pizza * 2.50 + pieces_of_chicken * 1.99;
else
cost = pieces_of_chicken * 1.99 + slices_of_pizza * 2.50;
end
end
then is it the implication that 3 would be associated with bottles_of_2L_softdrink and 2 would be associated with cartons_of_500ml_ice_cream because those are the first two variables mentioned in the code (in a statement never executed)? If so then how many slices_of_pizza and how many pieces_of_chicken ?
Is it the algorithm that you when encounter a variable that has not been assigned a value yet, that the variable will be assigned the value that is next on the parameter list? So if you had
function cost = fad4(3,2)
%each piece of chicken is accompanied by a finger cleaning packet
cost = pieces_of_chicken * 1.99 + peices_of_chicken * 0.03 + slices_of_pizza * 2.50;
end
then should pieces_of_chicken be associated with 3, and peices_of_chicken which is a distinct variable name to MATLAB should be associated with 2, and slices_of_pizza should be associated with ... what? MATLAB doesn't know that peices_of_chicken is a typing mistake for pieces_of_chicken ...
Can you come up with any sustainable principle for associating numeric values in a function declaration with particular variable names in the function body?
DGM
on 28 Oct 2023
See
For further reading, look at the bottom of those pages.
Categories
Find more on Data Type Identification 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!