MATLAB: Integrate inside ode45, every value of a a vector

integralode45

Hi everyone,
I have the following part inside a script
if t<=1.5
x=(0:2.51326406981556e-05:2*pi)'; %limits for integration
.
.
.
for i=1:1:250001
Y=(((r.*cos(x)-y(5))-r.*cos(x)+y(6))./((((((r.*cos(x)-y(5))-r.*cos(x)+y(6))).^2+((r.*sin(x)-y(5))+r.*sin(x)).^2).^0.5))); % r is difined in the beginning of the script
Integral(i)=trapz(x,Y);
end
.
.
.
y(5)=y(6);
dy(6)=(-kr*(y(5)+L*y(11))+b*(-kr*(y(6)+L*y(12)))-0.36*Fload*Integral(i))/mcd; % Fload is defined in the beginning.
I want after every time step, which is 1e-5, to use the values of y(5) and y(6) in order to calculate the Integral(i) and use this value for the next calculation of y(5) and y(6).
Practically, i want to integrate every value of Y, which is a function of y(5), y(6), inside [0,2pi], and y(5) and y(6) to be calculated based on this integral.
Is this the correct way for doing this?
Thank you very much.

Best Answer

no need of for loop
if t<=1.5
x=(0:2.51326406981556e-05:2*pi)'; %limits for integration
Y=(((r.*cos(x)-y(5))-r.*cos(x)+y(6))./((((((r.*cos(x)-y(5))-r.*cos(x)+y(6))).^2+((r.*sin(x)-y(5))+r.*sin(x)).^2).^0.5))); % r is difined in the beginning of the script
% C1 = r*cos(x) - y(5) + y(6) - r*cos(x);
% C2 = 2*r.*sin(x)-y(5);
% Y=C1./sqrt(C1.^2+C2.^2); % r is difined in the beginning of the script
IntegralY=trapz(x,Y);
end
Show the equations you use