查看: 90|回复: 1|关注: 0

[已答复] 带有逻辑判断函数的求导问题

[复制链接]

新手

5 麦片

财富积分


050


1

主题

1

帖子

0

最佳答案
发表于 2019-10-10 16:37:55 | 显示全部楼层 |阅读模式
自己根据文献中的函数形式编写了如下程序,存储为fai.m ,已经可调用并通过输入x,y计算出fai的值
问题1:但当使用diff(fai,y)进行求导(或某一特定点的求导)时,便出现问题,红字显示
fai (line 16)
if x<1&&y>yi(1,8)

但函数中有大量的参数需要通过逻辑判断进行值的确定

请问如何才能避免该问题,从而能顺利进行求导?
问题2:如何求在某一自变量趋近于0的情况下函数对另一自变量的偏导数?

源代码如下:(版本为R2017b)


function fai=f(x,y)
yi=[0,2.9690968,0.40556079,2.3253752,1.1134924,0.61194084,1.485456392,1.101493512];
Qi=[-1,0,1,3,21,24.83,250,21];
EJ=[1.5,1.94435064,1.46,1.02,0.983,1.173];
EJj=[-1.4020567,6.5384430,0,0
    -6.604778,8.9685638,-0.87538506,0.19264642];
Fij=[0.48176287,-1.17870687,-0.24051263,-0.0750908703,-0.213826595,-0.00701074245,-3.56560121
    -0.22996818,0.611458185,-0.357633417,-0.0394741659,0.85313703,0.0392623803,8.73657614
    0.67678089,-1.59127084,1.38545921,-0.0376038501,-1.77422585,-0.110956774,-11.83502506
    -0.82687853,2.18816951,-2.2662842,0.0404244668,2.11856977,0.175796531,9.56797096
    0.59684665,-1.66416239,1.89132269,-0.462622543,-1.53874755,-0.158135317,-4.6146467
    -0.26046265,0.741574506,-0.896367224,0.24081461,0.693737872,0.0752434365,1.22917766
    0.068249584,-0.192234256,0.244259366,-0.0668988147,-0.190153915,-0.0146753994,-0.13948161
    -0.0098938026,0.0268815303,-0.0356830831,0.00967590594,0.0290866982,0,0
    0.00060938476,-0.00156464723,0.00216704074,-0.000575094683,-0.00190789666,0,0];

%原始数据
if x<1&&y>yi(1,8)
    J=1;
else
    J=0;
end
if y>yi(1,5)
    K=1;
else
    K=0;
end
if y>yi(1,8)
    L=1;
else
    L=0;
end
if J==0
    mJ=1;
else
    mJ=3;
end

%逻辑判断与赋值

X0J=[];
for j=0:1:mJ
    X0J=[X0J;EJj(J+1,j+1)*x^(j)];
end
E_J=EJ(1,J+1);
X_0J=sum(X0J)-E_J*x*log(x);

u0=[];
for jj=(1:9)
u0=[u0;Fij(jj,1)*y^jj];
end
u_0=sum(u0);
Q_0=Qi(1,1);
X_0=x^(-Q_0);

u1=[];
    for jj=(1:1:9)
u1=[u1;Fij(jj,2)*y^jj];
    end
    u_1=sum(u1);
F_10J=-u_1*yi(1,J+1);
X_1=x^(-Qi(1,2));
uFX1=(u_1+F_10J)*X_1;

u2=[];
    for jj=(1:1:9)
u2=[u2;Fij(jj,3)*y^jj];
    end
    u_2=sum(u2);
F_20J=-u_2*yi(1,J+1);
X_2=x^(-Qi(1,3));
uFX2=(u_2+F_20J)*X_2;

u3=[];
    for jj=(1:1:9)
u3=[u3;Fij(jj,4)*y^jj];
    end
    u_3=sum(u3);
F_30J=-u_3*yi(1,J+1);
X_3=x^(-Qi(1,4));
uFX3=(u_3+F_30J)*X_3;

%参数计算

  if yi(1,3)<y<yi(1,4)
    x_4=y;
    elseif y<=yi(1,3)
        x_4=yi(1,3);
    elseif y>=yi(1,4)
        x_4=yi(1,4);
  end

   u4=[];
for jj=(1:9)
u4=[u4;Fij(jj,5)*(x_4)^jj];
end
u_4=sum(u4);
F_40=u_4*yi(1,5);
X_4K=x^(-Qi(1,4+K));
F_40J=F_40-u_4*yi(1,3+J);
X_4J=x^(-Qi(1,4+J));

if  L==0
    if yi(1,6)<y<yi(1,7)
    x_5m=y;
    elseif yi<=yi(1,6)
            x_5m=yi(1,6);
    end
end
  if  L==1
        if yi(1,6)<y<yi(1,7)
    x_5m=y;
        elseif yi>=yi(1,7)
                 x_5m=yi(1,7);
        end   
  end

  u5L=[];
  for jj=(1:9)
      u5L=[u5L;Fij(j,6+L)*(x_5m)^jj]
  end
  u_5L=sum(u5L);
  F_5L=u_5L*yi(1,6+L);
  if EJ(1,5)<x<EJ(1,6)
  X_5=EJ(1,3)*x^(-Qi(1,7))+EJ(1,4)*x^(-Qi(1,8));
  else
      X_5=0;
  end

fai=X_0J+x*log(y)+u_0*X_0+uFX1+uFX2+uFX3+(u_4-F_40)*X_4K+F_40J*X_4J+(u_5L-F_5L)*X_5;



论坛优秀回答者

权威

3873 麦片

财富积分



3

主题

4086

帖子

862

最佳答案
  • 关注者: 183
发表于 2019-10-10 17:26:32 | 显示全部楼层
参考:
syms x y(x) dy(x)
y(x) = piecewise(x<0, -x, x>0, x);
dy(x)=diff(y(x),x);
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

站长推荐上一条 /3 下一条

快速回复 返回顶部 返回列表