I need to use the quad method to integrate in a loop. I cant create and external file and call to it because the variables in the integration equation keep changing with the loop, so I cut the loop for the purpose of making is simple. It's giving me an error n the quad method line - don't know how to fix it - KrInitialIntegratedPart to be exact The error messages I get are:
Error using inline/subsref (line 13) Not enough inputs to inline function.
Error in quad (line 68) y = f(x, varargin{:});
Error in newproj4 (line 39) KrInitialIntegratedPart= quad('(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;
By file do you mean that a separate M-file that defines the function to be integrated? That's the old-fashioned way to use quad
and the quadrature integration functions anyways. You shouldn't be passing in strings but rather function handles. Assuming that x
is the integration variable, you can use this:
KrInitialIntegratedPart = quad(@(x)sin(x)*exp(-AInitial/aInitial)*db, 0, fe);
The parameters AInitial
, aInitial
, and db
will be determined by their current value in the code. (Also, you don't need to use so many parentheses – it makes reading the code harder.)
If you do write your integration function in a separate M-file or a sub-function, you should still use function handles. In that case, you can call the function and pass in the parameters by creating an anonymous function like this:
KrInitialIntegratedPart = quad(@(x)MyFunName(x, AInitial, aInitial,db), 0, fe);
Depending on the version of Matlab you're using, you might try integral
instead (or quadgk
if you don't have that).