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

305 ビュー (過去 30 日間)
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

採用された回答

Guillaume
Guillaume 2020 年 2 月 25 日
編集済み: Guillaume 2020 年 2 月 25 日
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 件のコメント
Michelle Gaughan
Michelle Gaughan 2020 年 2 月 25 日
numbers[c(i),parError(c(i), m, g)]
numbers2[pardError(c(i), m, g), c(i+1)-c(i)];
for these part not being valid syntax, what could i reply them with?
Guillaume
Guillaume 2020 年 2 月 25 日
編集済み: Guillaume 2020 年 2 月 25 日
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?

サインインしてコメントする。

その他の回答 (3 件)

Tshiabu Angelus Dan
Tshiabu Angelus Dan 2023 年 5 月 3 日
  2 件のコメント
Steven Lord
Steven Lord 2023 年 5 月 3 日
It's easier to comment on code if you post it as text rather than an image. Please post your code as text in future questions.
Anyway, you're missing the multiplication symbols in two places. Where you have (2/Ts)(1-z) you need * between those terms, (2/Ts)*(1-z). I think if you move the cursor over one of those inner parentheses on that line the Editor will indicate one of those parentheses in each of those terms are in columns 17 and 33 as stated in the error messages.
Tshiabu Angelus Dan
Tshiabu Angelus Dan 2023 年 5 月 3 日
Thank you so much Steven Lord in future, I'll post the quest as a text 🙏

サインインしてコメントする。


Tshiabu Angelus Dan
Tshiabu Angelus Dan 2023 年 5 月 5 日
import numpy as np
import matplotlib.pyplot as plt
define input signal x
x = np.sin(np.arange(0, 2*np.pi, 0.1));
calculate output signal y using digital differentiator
y = np.append*(x[0], np.diff(x))
plot input and output signals
plt.plot(x, label='input signal')
plt.plot(y, label='output signal')
plt.legend()
plt.xlabel('Sample number')
plt.ylabel('Amplitude')
plt.title('Digital Differentiator Output for a Sinusoidal Signal')
plt.show()
QUESTION1_5a
File: QUESTION1_5a.m Line: 8 Column: 16
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
QUESTION1_5a
File: QUESTION1_5a.m Line: 8 Column: 16
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
QUESTION1_5a
File: QUESTION1_5a.m Line: 8 Column: 17
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
  2 件のコメント
Steven Lord
Steven Lord 2023 年 5 月 5 日
The * between the name of the function (np.append) and the parentheses containing its input arguments looks suspicious.
Walter Roberson
Walter Roberson 2023 年 5 月 16 日
import numpy as np
In MATLAB, import does not accept an as clause.
define input signal x
There is no supplied MATLAB function named define -- but it is potentially possible to define a function with the name define that accepted multiple character vectors as parameters, so this is potentially a valid MATLAB statement.
calculate output signal y using digital differentiator
Same remarks as for define
y = np.append*(x[0], np.diff(x))
In MATLAB, [] cannot appear directly after a name. [] is used for list building (but not for indexing). A constructed list would need to have an operator between any previous part of the expression and the list.

サインインしてコメントする。


Tshiabu Angelus Dan
Tshiabu Angelus Dan 2023 年 5 月 16 日
編集済み: Walter Roberson 2023 年 5 月 16 日
Gp = 10^(-2/20); Gs = 10^(-12/20); wp1 = 120/(2*pi); wp2 = 300/(2*pi); ws1 = 45/(2*pi); ws2 = 450/(2*pi);
% Pre-warping Ts = 1/500; % choose highest sampling period Wp1 = tan(wp1*pi*Ts); Wp2 = tan(wp2*pi*Ts); Ws1 = tan(ws1*pi*Ts); Ws2 = tan(ws2*pi*Ts);
% Analog filter design Rp = -20*log10(Gp); Rs = -20*log10(Gs); [n, Wn] = cheb2ord([Wp1 Wp2], [Ws1 Ws2], Rp, Rs); [b, a] = cheby2(n, Rs, [Ws1 Ws2]);
% Digital filter design using bilinear transformation [bd, ad] = bilinear(b, a, 1/Ts);
% Filter the input signal using the designed filter t = 0:Ts:1; x = 1 + sin(10*pi*t) + sin(200*pi*t) + sin(400*pi*t); y = filter(bd, ad, x);
% Plot the input and output signals subplot(2,1,1) plot(t, x) title('Input Signal') xlabel('Time (s)') ylabel('Amplitude')
subplot(2,1,2) plot(t, y) title('Filtered Signal') xlabel('Time (s)') ylabel('Amplitude')
From this Design and code how can I plot the frequency response for Lowpass Chebyshev and Bandstop Chebyshev for Lowpass, Highpass Chebyshev and Bandpass Chebyshev for Highpass.
  1 件のコメント
Walter Roberson
Walter Roberson 2023 年 5 月 16 日
This does not appear to be an explanation of why you can get the "invalid expression" message. It does not appear to fit within the context of the current discussion.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeIntegrated Circuits についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by