我需要使用四元法来进行循环积分。我无法创建外部文件并调用它,因为积分方程中的变量随着循环不断变化,所以我切断了循环,以便使制作变得简单。它在四元方法行中给了我一个错误 - 不知道如何修复它 - 准确地说是 KrInitialIntegratedPart 我收到的错误消息是:
使用 inline/subsref 时出错(第 13 行) 内联函数的输入不足。
quad 中的错误(第 68 行) y = f(x, varargin{:});
newproj4 中的错误(第 39 行) KrInitialIntegratedPart= 四边形('(sin(x))(exp(-AInitial/aInitial))(db)', 0, 铁)
clear all;
clc;
%% All the constants and initial conditions
% the position conponents are "r" and "z"
% the velocity conponents are "vr" and "vz"
% the initial position components
rInitial= 10; %kpc
zInitial= 0; %kpc
% the initial velocity components
vrInitial= 0; %km/s
vzInitial= 150; %tangential velocity component
vtInitial= 150; %not used
% the height
h= rInitial*vzInitial; %angulan momentum constant
tInitial=0;
Dt=1e-3;
%ThetaiPlus1= Thetai + ( ( Dt*h ) / ( riPlus1Over2^2 ) );
%% The for loop
% the position components
riPlus1Over2= rInitial + 0.5*Dt*vrInitial;
ziPlus1Over2= zInitial + 0.5*Dt*vzInitial;
% integrating to solve for the acceleration components
% needed for the integration
b=0;
e=0.99;
pc=11613.5;
AInitial= (1/e)*sqrt( ( rInitial^2*( sin(0) )^2 ) + ( zInitial^2*( tan(0) )^2 ) );
aInitial=2.8;
fe=asind(e); % the upper limit of th integral
db= fe / 20; %4.094519277200290
% solving for the accleration compoenet in the r diection
KrInitialIntegratedPart= quad('(sin(x))*(exp(-AInitial/aInitial))*(db)', 0, fe)
KrInitialFirstPart= -4*pi*pc*sqrt( 1-(e^2) / (e^3) )*rInitial;
KrInitial= KrInitialFirstPart*KrInitialIntegratedPart;
文件是指定义要集成的功能的单独的M文件吗?无论如何,这是使用
quad
和正交积分函数的老式方法。您不应该传递字符串,而应该传递“函数句柄”。假设 x
是积分变量,你可以使用这个:KrInitialIntegratedPart = quad(@(x)sin(x)*exp(-AInitial/aInitial)*db, 0, fe);
参数
AInitial
、
aInitial
和 db
将由它们在代码中的当前值确定。 (此外,您不需要使用这么多括号 - 这会使阅读代码变得更加困难。)如果您确实将集成函数编写在单独的 M 文件或子函数中,则仍应使用函数句柄。在这种情况下,您可以通过创建一个匿名函数
来调用该函数并传入参数,如下所示:
KrInitialIntegratedPart = quad(@(x)MyFunName(x, AInitial, aInitial,db), 0, fe);
根据您使用的 Matlab 版本,您可以尝试使用
(或者
quadgk
,如果您没有)。