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

for Y(1) =1:1:numel(t)
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.

Réponses (4)

Y(1) is a scalar (single number), 1:numel(t) is a vector. You can't set a scalar to a vector.
Use an unused variable name instead, e.g.
for k = 1:numel(t)
Try
for k = 1 : numel(t)
this_t = t(k);
Y(k) = 5 * this_t ^ 2 + 42; % Replace with whatever your formula for Y is.
end

5 commentaires

h=3600;
A0=1;
B0=3;
P0=0;
K=5*10^-5;
Yb=1;
Yp=0.15;
tspan = [0 43200];
Fyt = @(t,y) [(-K*Y(1)*Y(2));
(-Yb*(K*Y(1)*Y(2)));
(Yp*(K*Y(1)*Y(2)))];
for k=1 : numel(t)
k1 = fyt ((Y(1),Y(2));
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.

Error in connector.internal.fevalMatlab

Error in connector.internal.fevalJSON
k2 = fyt ((Y(1)+0.5*h,Y(2)+0.5*h*k1));
k3 = fyt ((Y(1)+0.5*h,Y(2)+0.5*h*k2));
k4 = fyt((Y(1)+h),(Y(2)+h*k3));
Y(i+1) = Y(2) + (h/6)*(k1+2*k2+2*k3+k4);
figure (1)
plot(t,Y(:,1))
figure (2)
plot(t,Y(:,2))
figure (3)
plot(t,Y(:,3))
end
k1 = fyt ((Y(1),Y(2));
12 3 2 3 21
You have one more ( than you have )
Fyt = @(t,y) [(-K*y(1)*y(2));
(-Yb*(K*y(1)*y(2)));
(Yp*(K*y(1)*y(2)))];
instead of
Fyt = @(t,y) [(-K*Y(1)*Y(2));
(-Yb*(K*Y(1)*Y(2)));
(Yp*(K*Y(1)*Y(2)))];
Which error message do you get? Avoid overdoing the parentheses:
Fyt = @(t,Y) [-K * Y(1) * Y(2); ...
-Yb * K * Y(1) * Y(2); ...
Yp * K * Y(1) * Y(2)];
This looks cleaner.
Note that you have defined "Fyt", but call "fyt" later.
This cannot work also:
k2 = fyt ((Y(1)+0.5*h, Y(2)+0.5*h*k1));
% ^ ^
Here the inner parentheses combine the two arguments, but the function Fyt (not fyt) should get 2 arguments. Better:
k2 = Fyt(Y(1) + 0.5 * h, Y(2) + 0.5 * h * k1);

Connectez-vous pour commenter.

h=3600;
A0=1;
B0=3;
P0=0;
K=5*10^-5;
Yb=1;
Yp=0.15;
t = 0:0.1:10;
fyt = @(t,y) [(-K*y(1)*y(2));
(-Yb*(K*y(1)*y(2)));
(Yp*(K*y(1)*y(2)))];
Y = zeros(3,numel(t))
Y(1,1) = 1.0;
Y(2,1) = 3.0;
Y(3,1) = 0;
for i=1 : numel(t)-1
k1 = fyt(t(i),Y(:,i));
k2 = fyt(t(i)+0.5*h,Y(:,i)+0.5*h*k1);
k3 = fyt(t(i)+0.5*h,Y(:,i)+0.5*h*k2);
k4 = fyt(t(i)+h,Y(:,i)+h*k3);
Y(:,i+1) = Y(:,i) + (h/6)*(k1+2*k2+2*k3+k4);
end
figure (1)
plot(t,Y(1,:))
figure (2)
plot(t,Y(2,:))
figure (3)
plot(t,Y(3,:))

4 commentaires

Look at the results. They won't change after t=10 (unless you change the function to be integrated).

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB dans Centre d'aide et File Exchange

Produits

Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by