求助:
本人matlab菜鸟,在用fsolve程序解非线性方程时遇到问题,具体程序、数据以及所用方程如附件。
程序以及提示错误如下,百思不得其解,求解答!!
function x=Problem_1()
clear all
% x是100个r的解向量
% 把函数Problem_1()文件和ProblemData_1数据保存放在桌面或者指定的matlab搜索文件夹(Current Folder设置)下
% 在Command Window里面输入x=Problem_1()即可得到x
clc
load matlab1111
x=zeros(1,length(P));
% 初始值
r0=0;
for i=1:length(P)
x(i)=fsolve(@(r)myfun(r,P(i),B(i,:),FROE(i,:)),r0);
end
x=x';
end
% 求解的r的函数
function y = myfun(r,P,B,FROE)
F=B(1);
for i=1:12
if i<12
F=F+(FROE(i)-r)*B(i)/((1+r)^i);
else
F=F+(FROE(i)-r)*B(i)/(r*(1+r)^i);
end
end
y=P-F;
% 求解y等于0
end
提示错误:
错误使用 trustnleqn (line 28)
Objective function is returning undefined values at initial point. FSOLVE cannot continue.
出错 fsolve (line 366)
[x,FVAL,JACOB,EXITFLAG,OUTPUT,msgData]=...
出错 Problem_1 (line 12)
x(i)=fsolve(@(r)myfun(r,P(i),B(i,:),FROE(i,:)),r0);
附件列表