Matlab - 用2个变量计算函数的微分:如何指定2个变量的依赖性

时间:2018-02-01 05:11:37

标签: matlab

我尝试根据2个变量计算函数的微分:v和t

符号函数定义为:

d=100;
syms f g h c x v t alpha
alpha=1;
c=4/100

g=c*(v/90)^alpha;
h=diff(g,v);
m=c*(v/90)^alpha*v*t;

%% Derivatives for differential
firstTerm=diff(m,v);
secondTerm=diff(m,t);
firstTermFunction=matlabFunction(firstTerm);
secondTermFunction=matlabFunction(secondTerm);

% Direct computation of integral
f=int(2*d/(2*v^2-h*v^4),v);

vInit=50;
vMax=500;

j=matlabFunction(f);
htMatlab=matlabFunction(f-j(vInit));

v_start=vInit;
step=0.01;
v_final=vMax;
vNew=v_start:step:v_final;

% Function to integrate
htIntegral=@(v)integral(htMatlab,v_start,v); 
% Integrate numerically
ht=abs(arrayfun(htIntegral,vNew);

% Take only positive values for ht(vNew) = time
j_sorted=find(ht<10);
t_sorted(1:numel(j_sorted))=ht(j_sorted);

% Set sum of diff to 0
sumDiff=0;
i=0;
while ((sumDistance<d) && (i<(numel(vNew)-1)))
   i=i+1;
sumDistance=sumDistance+vLast(i)*(t_sorted(i+1)-t_sorted(i));
sumDiff=sumDiff+firstTerm(vLast(i),t_sorted(i))*(vLast(i+1)-vLast(i))+secondTerm(vLast(i),t_sorted(i))*(t_sorted(i+1)-t_sorted(i));          
end

我的问题是关于函数firstTermFunctionsecondTermFunction

如您所见,两者都取决于变量vt

我想在点t=t_sorted(i)v_i=vLast(i)上评估这些功能,但我不知道如何执行此操作,因为我不知道如何指定依赖性两者的vt

如果有人可以帮助我如何评估Matlab函数firstTermFunctionsecondTermFunction以及使用的语法:

我尝试直接执行此操作:firstTermFunction(vLast(i),t_sorted(i))secondTermFunction(vLast(i),t_sorted(i))但我收到错误。

此致

更新1:我的问题可能来自使用matlabFunction将符号转换为数字函数:实际上,我没有在转换后指定依赖性。

更新2:使用以下语法:

firstTermFunction=@(v,t) matlabFunction(firstTerm);

我开始执行:

>> firstTermFunction

firstTermFunction =

  function_handle with value:

    @(v,t)matlabFunction(firstTerm)

如果我现在测试值firstTerm(1,1)(即v=1t=1),我得到:

>> firstTermFunction(1,1)

ans =

  function_handle with value:

    @(t,v)t.*sqrt(v.*(1.0./9.0e1)).*(1.0./2.5e1)+t.*v.*1.0./sqrt(v.*(1.0./9.0e1)).*2.222222222222222e-4

这不是我所期望的:事实上,变量vt并未被v=1t=1数值替换。

0 个答案:

没有答案
相关问题