substituting function variable in function handle
14 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
i want to substitute y(5) in place of theta and poceed for the ode calculations.
Here is my script
syms theta
A0 = cos(theta).*[1 5 6; 2 9 3; 1 4 5];
A =[A0 zeros(3, 2);
zeros(2, 3) [1 0; 0 1]]
B = tan(2*theta).*[8 5 1 0 0; 1 1 6 0 0; 2 4 7 0 0; 0 0 0 0 0; 0 0 0 0 0]
C = (cos(2*theta)+2).*[8 5 1; 0 1 6; 2 4 7];
myfun = @(t,y)scriptname(t,y,A,B,C);
% dummy values for tspan and y0
tspan = [0 1];
y0 = zeros(1, 5);
An = matlabFunction(subs(A, y(5)))
Bn = matlabFunction(subs(B, y(5)))
Cn = matlabFunction(subs(C, y(5)))
sol = ode45(myfun,tspan,y0);
h = figure;
plot(sol.x,sol.y(i,:));
function dydt = scriptname(t,y,An,Bn,Cn)
inertia = 0.05;
Wr = 2*pi*50;
p =2;
% Cn = double(subs(C,y(5)))
for i=1:3
I(i,1)=y(i);
end
I
Te = 1/2*p*I'*Cn*I
if t<0.75
Tl=0;
else
Tl=7.31;
end
V = [1.4142*400/sqrt(3)*cos(Wr*t);
1.4142*400/sqrt(3)*cos(Wr*t+2.*pi/3.);
1.4142*400/sqrt(3)*cos(Wr*t-2.*pi/3.);
(Te-Tl)/inertia;
y(4)]
% An = double(subs(A,y(5)))
% Bn = double(subs(B,y(5)))
dydt = An\V-(Bn*y);
end
It is showing the error of "unrecognized function or variable y".
3 comentarios
Respuestas (1)
Jaynik
el 2 de Abr. de 2024
Editada: Jaynik
el 3 de Abr. de 2024
Hi Bathala,
The error you are encountering occurs because "y" is not defined in the scope where you are trying to substitute "y(5)" for "theta" in matrices "A", "B", and "C". To fix this issue, "y" must first be declared as a symbolic variable. that represents a vector, and then perform the substitution.
You can refer to the following code:
syms theta
syms y [5 1]
A0 = cos(theta).*[1 5 6; 2 9 3; 1 4 5];
A =[A0 zeros(3, 2); zeros(2, 3) [1 0; 0 1]];
B = tan(2*theta).*[8 5 1 0 0; 1 1 6 0 0; 2 4 7 0 0; 0 0 0 0 0; 0 0 0 0 0];
C = (cos(2*theta)+2).*[8 5 1; 0 1 6; 2 4 7];
% Substitute theta with y(5) in A, B, C
A_sub = subs(A, theta, y(5));
B_sub = subs(B, theta, y(5));
C_sub = subs(C, theta, y(5));
% Convert the substituted expressions to MATLAB functions
An = matlabFunction(A_sub, 'Vars', {y});
Bn = matlabFunction(B_sub, 'Vars', {y});
Cn = matlabFunction(C_sub, 'Vars', {y});
function dydt = scriptname(t, y, An, Bn, Cn)
inertia = 0.05;
Wr = 2*pi*50;
p = 2
% Using the An, Bn, and Cn functions with the current state vector y
A_current = An(y);
B_current = Bn(y);
C_current = Cn(y);
I = y(1:3);
Te = 1/2 * p * I' * C_current * I;
if t < 0.75
Tl = 0;
else
Tl = 7.31;
end
V = [1.4142*400/sqrt(3)*cos(Wr*t);
1.4142*400/sqrt(3)*cos(Wr*t+2*pi/3);
1.4142*400/sqrt(3)*cos(Wr*t-2*pi/3);
(Te - Tl) / inertia;
y(4)];
dydt = A_current \ V - (B_current * y);
end
You can refer the following documentation to read more about these functions:
- subs: https://www.mathworks.com/help/releases/R2021a/symbolic/subs.html
- matlabFunction: https://www.mathworks.com/help/releases/R2021a/symbolic/matlabfunction.html
Hope this helps!
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!