matlab-求解非线性方程/方程组

综述

使用matlab求解非线性规划

代码

目标函数们 == 0

%create by Zhiyang Dou
%2019/4/10
%contact:[email protected]
function F = target(D,r,J,B,sigma_J)

F(1)= -J + ((J+D(1))*normcdf((log((J+D(1))./B)+ (r+0.5*D(2)*D(2)))./D(2))-B*exp(-r)*normcdf((log(J+D(1)) + (r+0.5*D(2)*D(2)))./D(2) - D(2)));
F(2)= -sigma_J+(J+D(1))*normcdf((log((J+D(1))./B)+ (r+0.5*D(2)*D(2)))./D(2))*D(2)./J;
  

(注解版)


%create by Zhiyang Dou
%2019/4/10
%contact:[email protected]
function F = target(D,r,J,B,sigma_J)%函数需要传入参数D,r,J,B,sigma_J

F(1)= -J + ((J+D(1))*normcdf((log((J+D(1))./B)+ (r+0.5*D(2)*D(2)))./D(2))-B*exp(-r)*normcdf((log(J+D(1)) + (r+0.5*D(2)*D(2)))./D(2) - D(2)));
F(2)= -sigma_J+(J+D(1))*normcdf((log((J+D(1))./B)+ (r+0.5*D(2)*D(2)))./D(2))*D(2)./J;
%由于是两个方程,所以F(1) F(2) 对应我们的公式7、9

求解器

%create by Zhiyang Dou
%2019/4/10
%contact:[email protected]
res_con = zeros(36,6);
 A = xlsread('jinrongbumen.xlsx', 'Sheet1', 'A3:E38');
 [m,n] = size(A);
 for i  = 1:m
options=optimset('Display','off');
J = A(i,1);
r = A(i,3);
B = A(i,2);
sigma_J = A(i,4);
[D]=fsolve(@(D)target(D,r,J,B,sigma_J),[1;0.7]',options) %求解器
res_con(i,1) = D(1); %第一列是D
res_con(i,2) = D(2); %sigma_a
res_con(i,3) = sigma_J * J /( D(2)*normcdf((log((J+D(1))./B)+ (r+0.5*D(2)*D(2)))./D(2))  ); %A
res_con(i,4) = (log((J+D(1))./B)+ (r+0.5*D(2)*D(2)))./D(2); %d_1
res_con(i,5) = res_con(i,4) - D(2); %d_2
res_con(i,6) = normcdf(res_con(i,5)); %N(-d_2)
 end
 xlswrite('/Users/frankdura/Downloads/res.xls', res_con); %结果保存到csv或者excel
 
 

(注解版)

%create by Zhiyang Dou
%2019/4/10
%contact:[email protected]
res_con = zeros(36,6); %初始化结果存储区
 A = xlsread('jinrongbumen.xlsx', 'Sheet1', 'A3:E38');  %读取相关的excel文件
 [m,n] = size(A);%获取行列数
 for i  = 1:m  %按照行进行遍历
options=optimset('Display','off');
J = A(i,2); %获得第i行第2列数据
r = A(i,4);
B = A(i,3);
sigma_J = A(i,5);  
[D]=fsolve(@(D)target(D,r,J,B,sigma_J),[1;0.7]',options) %求解器
res_con(i,1) = D(1); %第一列是D
res_con(i,2) = D(2); %sigma_a
res_con(i,3) = sigma_J * J /( D(2)*normcdf((log((J+D(1))./B)+ (r+0.5*D(2)*D(2)))./D(2))  ); %A
res_con(i,4) = (log((J+D(1))./B)+ (r+0.5*D(2)*D(2)))./D(2); %d_1
res_con(i,5) = res_con(i,4) - D(2); %d_2
res_con(i,6) = normcdf(res_con(i,5)); %N(-d_2)
 end
 xlswrite('/Users/frankdura/Downloads/res.xls', res_con); %结果保存到csv或者excel

你可能感兴趣的:(数学,Matlab)