Need help to make a ode function asign a value
1 次查看(过去 30 天)
显示 更早的评论
Hello everyone, I am trying to integrate a function with Ode45, but I'm having trouble making the function identify a value. I'll post part of the code below to explain it better. The code is divided in three different files, summing up the first file we have:
Mat_L=[zeros(n,n),eye(n,n); -A\(rho*V*V*C+E), -A\(rho*V*B+D)];
Mat_NL=[-ka3*E(2,2)*inv(A)*[0; 1 ;0], zeros(3,5); zeros(3,6)];
the second and third files contain:
function dxdt = tempo_SysEDO_L(t,x,V)
[Mat_L,Mat_NL] = base(ka3,V);
Mat3 = Mat_L*[x(1); x(2); x(3); x(4); x(5); x(6)];
dx1dt = Mat3(1,:);
dx2dt = Mat3(2,:);
dx3dt = Mat3(3,:);
dx4dt = Mat3(4,:);
dx5dt = Mat3(5,:);
dx6dt = Mat3(6,:);
dxdt = [dx1dt; dx2dt; dx3dt; dx4dt; dx5dt; dx6dt];
end
and
function dxdt = tempo_SysEDO_NL(t,x,V)
ka3 = 0.002;
[Mat_L,Mat_NL] = base(ka3,V);
Mat3 = Mat_L*[x(1); x(2); x(3); x(4); x(5); x(6)]+ Mat_NL*[x(1); x(2).^3; x(3); x(4); x(5); x(6)];
dx1dt = Mat3(1,:);
dx2dt = Mat3(2,:);
dx3dt = Mat3(3,:);
dx4dt = Mat3(4,:);
dx5dt = Mat3(5,:);
dx6dt = Mat3(6,:);
dxdt = [dx1dt; dx2dt; dx3dt; dx4dt; dx5dt; dx6dt];
The last one:
V = 118;
[tn,xn] = ode45(@tempo_SysEDO_NL,[0,15],[0.02 0 0 0 0 0]');
[t,x] = ode45(@tempo_SysEDO_L,[0,15],[0.02 0 0 0 0 0]');
But it keeps saying "not enought imput arguments", and it's probably because the ode function is not assigning the value of V. How can I fix that?
0 个评论
回答(3 个)
Torsten
2023-12-13
A,,B,C,D,E,rho and n are undefined.
And you must supply V to your ode-function using
[tn,xn] = ode45(@(t,x)tempo_SysEDO_NL(t,x,V),[0,15],[0.02 0 0 0 0 0]');
[t,x] = ode45(@(t,x)tempo_SysEDO_L(t,x,V),[0,15],[0.02 0 0 0 0 0]');
instead of
[tn,xn] = ode45(@tempo_SysEDO_NL,[0,15],[0.02 0 0 0 0 0]');
[t,x] = ode45(@tempo_SysEDO_L,[0,15],[0.02 0 0 0 0 0]');
V = 118;
[tn,xn] = ode45(@(t,x)tempo_SysEDO_NL(t,x,V),[0,15],[0.02 0 0 0 0 0]');
[t,x] = ode45(@(t,x)tempo_SysEDO_L(t,x,V),[0,15],[0.02 0 0 0 0 0]');
function [Mat_L,Mat_NL] = base(ka3,V)
Mat_L=[zeros(n,n),eye(n,n); -A\(rho*V*V*C+E), -A\(rho*V*B+D)];
Mat_NL=[-ka3*E(2,2)*inv(A)*[0; 1 ;0], zeros(3,5); zeros(3,6)];
end
function dxdt = tempo_SysEDO_L(t,x,V)
[Mat_L,Mat_NL] = base(ka3,V);
Mat3 = Mat_L*[x(1); x(2); x(3); x(4); x(5); x(6)];
dx1dt = Mat3(1,:);
dx2dt = Mat3(2,:);
dx3dt = Mat3(3,:);
dx4dt = Mat3(4,:);
dx5dt = Mat3(5,:);
dx6dt = Mat3(6,:);
dxdt = [dx1dt; dx2dt; dx3dt; dx4dt; dx5dt; dx6dt];
end
function dxdt = tempo_SysEDO_NL(t,x,V)
ka3 = 0.002;
[Mat_L,Mat_NL] = base(ka3,V);
Mat3 = Mat_L*[x(1); x(2); x(3); x(4); x(5); x(6)]+ Mat_NL*[x(1); x(2).^3; x(3); x(4); x(5); x(6)];
dx1dt = Mat3(1,:);
dx2dt = Mat3(2,:);
dx3dt = Mat3(3,:);
dx4dt = Mat3(4,:);
dx5dt = Mat3(5,:);
dx6dt = Mat3(6,:);
dxdt = [dx1dt; dx2dt; dx3dt; dx4dt; dx5dt; dx6dt];
end
0 个评论
Star Strider
2023-12-13
There are several problems (not the least of which is the absence of the necessary variables, so it is not possible to run your code).
This should probably be something like:
V = 118;
[tn,xn] = ode45(@(t,x)tempo_SysEDO_NL(t,x,V,ka3,n,rho,A,B,C,D,E),[0,15],[0.02 0 0 0 0 0]');
[t,x] = ode45(@(t,x)tempo_SysEDO_L(t,x,V,ka3,n,rho,A,B,C,D,E),[0,15],[0.02 0 0 0 0 0]');
function [Mat_L,Mat_NL] = base(ka3,V,n,rho,A,B,C,D,E)
Mat_L=[zeros(n,n),eye(n,n); -A\(rho*V*V*C+E), -A\(rho*V*B+D)];
Mat_NL=[-ka3*E(2,2)*inv(A)*[0; 1 ;0], zeros(3,5); zeros(3,6)];
end
function dxdt = tempo_SysEDO_L(t,x,V,ka3,n,rho,A,B,C,D,E)
[Mat_L,Mat_NL] = base(ka3,V,n,rho,A,B,C,D,E);
Mat3 = Mat_L*x;
dx1dt = Mat3(1,:);
dx2dt = Mat3(2,:);
dx3dt = Mat3(3,:);
dx4dt = Mat3(4,:);
dx5dt = Mat3(5,:);
dx6dt = Mat3(6,:);
dxdt = [dx1dt; dx2dt; dx3dt; dx4dt; dx5dt; dx6dt];
end
function dxdt = tempo_SysEDO_NL(t,x,V,n,rho,A,B,C,D,E)
ka3 = 0.002;
[Mat_L,Mat_NL] = base(ka3,V,n,rho,A,B,C,D,E);
Mat3 = Mat_L*[x(1); x(2); x(3); x(4); x(5); x(6)]+ Mat_NL*[x(1); x(2).^3; x(3); x(4); x(5); x(6)];
dx1dt = Mat3(1,:);
dx2dt = Mat3(2,:);
dx3dt = Mat3(3,:);
dx4dt = Mat3(4,:);
dx5dt = Mat3(5,:);
dx6dt = Mat3(6,:);
dxdt = [dx1dt; dx2dt; dx3dt; dx4dt; dx5dt; dx6dt];
end
I still may have overlooked some details, however that should get you closer. All the function arguments need to be defined in your workspace (or within the functions tthemselves, as was ‘ka3’ in ‘tempo_SysEDO_NL’) prior to using them.
I leave the rest to you.
.
0 个评论
Sam Chak
2023-12-14
As mentioned by others, we cannot directly test your code because some information about the parameters is not provided. However, I have provided some fictional values for the rigid parameters and the simulation settings that may fit into your context for the purpose of testing the corrected code for the input arguments.
% ------ Beginning of Script ------
%% flexible parameters (values that may change in subsequent simulations)
V = 118; % defined by user
ka3 = 0.002; % defined by user
%% call ode45
tspan = [0 0.2]; % simulation time
x0 = [1; 0; 0; 0; 0; 0]; % initial values
[t, xL] = ode45(@(t, x) tempo_SysEDO_L(t, x, ka3, V), tspan, x0);
[t, xN] = ode45(@(t, x) tempo_SysEDO_NL(t, x, ka3, V), tspan, x0);
%% Plot the results
subplot(211)
plot(t, xL), grid on, title('Linear results')
subplot(212)
plot(t, xN), grid on, title('Nonlinear results')
% ------ The End of Script ------
%% Place the codes of the 3 functions at the end of the script
%% A base function to define the state matrices
function [Mat_L, Mat_NL] = base(ka3, V)
%% rigid parameters (values that do not change in subsequent simulations)
n = 3; % I guess from Mat3
rho = 1; % fictional value
A = eye(n); % fictional matrix
B = A; % fictional matrix
C = B; % fictional matrix
D = magic(3); % fictional matrix
E = D; % fictional matrix
%% formulas for matrices
Mat_L = [zeros(n), eye(n); -A\(rho*V*V*C+E), -A\(rho*V*B+D)];
Mat_NL = [-ka3*E(2,2)*inv(A)*[0; 1 ;0], zeros(3,5); zeros(3,6)];
end
%% Linear EDO
function dxdt = tempo_SysEDO_L(t, x, ka3, V)
%% get matrices from base()
[Mat_L, Mat_NL] = base(ka3, V);
%% construct the dynamical equations
% Mat3 = Mat_L*x;
% dx1dt = Mat3(1,:);
% dx2dt = Mat3(2,:);
% dx3dt = Mat3(3,:);
% dx4dt = Mat3(4,:);
% dx5dt = Mat3(5,:);
% dx6dt = Mat3(6,:);
% dxdt = [dx1dt; dx2dt; dx3dt; dx4dt; dx5dt; dx6dt];
dxdt = Mat_L*x; % dynamics expressed in matrix form
end
%% Nonlinear EDO
function dxdt = tempo_SysEDO_NL(t, x, ka3, V)
%% get matrices from base()
[Mat_L, Mat_NL] = base(ka3, V);
%% construct the dynamical equations
% Mat3 = Mat_L*[x(1); x(2); x(3); x(4); x(5); x(6)]+ Mat_NL*[x(1); x(2).^3; x(3); x(4); x(5); x(6)];
% dx1dt = Mat3(1,:);
% dx2dt = Mat3(2,:);
% dx3dt = Mat3(3,:);
% dx4dt = Mat3(4,:);
% dx5dt = Mat3(5,:);
% dx6dt = Mat3(6,:);
% dxdt = [dx1dt; dx2dt; dx3dt; dx4dt; dx5dt; dx6dt];
dxdt = Mat_L*x + Mat_NL*[x(1); x(2).^3; x(3); x(4); x(5); x(6)];
end
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!