how to fix error message Invalid expression as When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
Ältere Kommentare anzeigen
% Define the parameters
n = 25; % number of state variables
vz = 0.5 * ones(1, n); % vz_i values (constant)
rho = 0.2 * ones(1, n); % rho_alpha_i values (constant)
T = 500 * ones(1, n); % T_i values (constant)
Dr_alpha_i = 0.0045 * ones(n, 6); % D_r,alpha_i values (constant)
% Set the initial condition x0
x0 = zeros(n, 1);
% Set the time grid for integration
tspan = [0, 10]; % example: integrate from t=0 to t=10
% Set options for ode15s solver
opts = odeset('RelTol', 1e-6, 'AbsTol', 1e-9);
% Solve the system of equations using ode15s
[t, sol] = ode15s(@dxdt, tspan, x0, opts);
% Extract the state variables from the solution
x = sol(:, 1:n);
% Plot the results
figure;
plot(t, x);
xlabel('Time');
ylabel('State Variables');
legend('x_1', 'x_2', 'x_3', ... (list all the state variables), 'x_n');
title('State Variables vs. Time');
% Define the inputs u1(t) and u2(t)
function u1_val = u1(t)
% Define the input u1(t) as a constant value
u1_val = 650;
end
function u2_val = u2(t)
% Define the input u2(t) as a constant value
u2_val = 650;
end
% Define the system dynamics
function x_dot = dxdt(t, x)
% Extract state variables from x
x_i = x(1:n);
% Compute A1(x(t)) and B1(x(t))
A1 = diag(-vz.^(n+0.5));
B1 = Dr_alpha_i;
% Compute B2 and f(x(t))
B2 = diag(zeros(1, n));
f = [(-vz(1:end-1).^(n+0.5)).*(rho.^(n+0.5)), (-vz(1:end-1).^(n+0.5)).*(T.^(n+0.5))];
% Compute x_dot
u1_val = u1(t); % Call u1(t) function handle with parentheses
u2_val = u2(t); % Call u2(t) function handle with parentheses
x_dot = A1*x_i + B1*u1_val + B2*u2_val + f';
end
after running thecode i am geting error message even how can i fixthe error?
Error message:
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
2 Kommentare
1 - You need to define the variables inside the ODE function or either pass them as input arguements. I have edited the code with the former option.
2 - There's a dimension mis-match while defining f (as the error states as well). Since I do not know what you are trying to do, I can not offer any suggestion.
n=25;
% Set the initial condition x0
x0 = zeros(n, 1);
% Set the time grid for integration
tspan = [0, 10]; % example: integrate from t=0 to t=10
% Set options for ode15s solver
opts = odeset('RelTol', 1e-6, 'AbsTol', 1e-9);
% Solve the system of equations using ode15s
[t, sol] = ode15s(@(t,y) dxdt(t,y,n), tspan, x0, opts);
% Extract the state variables from the solution
x = sol(:, 1:n);
% Plot the results
figure;
plot(t, x);
xlabel('Time');
ylabel('State Variables');
%legend('x_1', 'x_2', 'x_3', ... (list all the state variables), 'x_n');
title('State Variables vs. Time');
% Define the inputs u1(t) and u2(t)
function u1_val = u1(t)
% Define the input u1(t) as a constant value
u1_val = 650;
end
function u2_val = u2(t)
% Define the input u2(t) as a constant value
u2_val = 650;
end
% Define the system dynamics
function x_dot = dxdt(t, x, n)
rho = 0.2 * ones(1, n); % rho_alpha_i values (constant)
T = 500 * ones(1, n); % T_i values (constant)
Dr_alpha_i = 0.0045 * ones(n, 6); % D_r,alpha_i values (constant)
% Extract state variables from x
x_i = x(1:n);
vz = 0.5 * ones(1, n); % vz_i values (constant)
% Compute A1(x(t)) and B1(x(t))
A1 = diag(-vz.^(n+0.5));
B1 = Dr_alpha_i;
% Compute B2 and f(x(t))
B2 = diag(zeros(1, n));
f = [(-vz(1:end-1).^(n+0.5)).*(rho.^(n+0.5)); (-vz(1:end-1).^(n+0.5)).*(T.^(n+0.5))];
% Compute x_dot
u1_val = u1(t); % Call u1(t) function handle with parentheses
u2_val = u2(t); % Call u2(t) function handle with parentheses
x_dot = A1*x_i + B1*u1_val + B2*u2_val + f';
end
RITIKA Jaiswal
am 13 Apr. 2023
Akzeptierte Antwort
Weitere Antworten (1)
Jon
am 13 Apr. 2023
The specific error you get is for line 22 where you have
legend('x_1', 'x_2', 'x_3', ... (list all the state variables), 'x_n');
You never close the parenthesis, you just have a ... followed by some texts. MATLAB is telling you that you need to have a right hand parenthesis to close the expression. Once you fix this your code has additional bugs. If you can't resolve these please tell us where you are stuck.
1 Kommentar
RITIKA Jaiswal
am 13 Apr. 2023
Kategorien
Mehr zu Programming finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!