我需要使用quad方法来集成在一个循环中。我不能创建和外部文件和调用它,因为在积分方程中的变量不断变化的循环,所以我削减循环的目的是做起来很简单。它给了我四边形方法行中的一个错误--不知道如何修复它--准确地说,是KrInitialIntegratedPart,我得到的错误消息是:
使用内联/subsref(第13行)时出错,没有足够的输入到内联函数。
四次误差(第68行)y= f(x,varargin{:});
newproj4 (第39行) KrInitialIntegratedPart=四(‘(sin(X))(exp(-aInitial/aInitial))(Db)’,0,fe)中的错误
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;
发布于 2013-12-19 08:37:42
您的意思是定义要集成的函数的单独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);
https://stackoverflow.com/questions/20686647
复制