我需要求解一个非线性方程组;为了使用,,我编写了一个m文件,其中包含了我的函数"myfun“。这个函数由主m文件调用.
系统和未知数都必须使用"for“循环编写。
示例:
function F=myfun(x)
n=20;`
for j=1:n
c1=sqrt(x(j)^2-3*x(j));
c2=x(j)^(1/2);
F(j)=c1+c2;
end我的问题是,我必须为向量预先分配内存,包括F和x,否则求解者会考虑numel(x)=1。但是如果我声明
F=zeros(n,1);
x=zeros(n,1);我有以下输出:
No solution found.
fsolve stopped because the problem appears regular as measured by the gradient,
but the vector of function values is not near zero as measured by the
default value of the function tolerance.有什么建议吗?谢谢
发布于 2014-10-16 14:53:31
你不需要循环,只需使用
F = sqrt(x.^2-3*x) + x.^(1/2);然后您也不需要声明n,c1,c2。
您的错误消息听起来不像是分配方面的问题,但更重要的是找到了解决问题的方法。
https://stackoverflow.com/questions/26407309
复制相似问题