what is wrong with my code

t=load('t_4l');
vrms=load('vrms_4l');
v0=[45, 47, 70,80, 65];
[v,fi]=lsqnonlin(@(v)fun(v,vrms,t),v0);
function e=fun(v,vrms,t)
e=[vrms- [repmat(v(1),1,6); sqrt((t(2,:)*v(2)^2+t(1,:)*v(1)^2)./(t(2,:)+t(1,:))); ...
sqrt((t(2,:)*v(2)^2+t(3,:)*v(3)^2)./(t(2,:)+t(3,:))); ...
sqrt((t(4,:)*v(4)^2+t(3,:)*v(3)^2)./(t(4,:)+t(3,:))); ...
sqrt((t(4,:)*v(4)^2+t(5,:)*v(5)^2)./(t(4,:)+t(5,:)))]];
t and vrms are all 5*6 matrix
The error is the function, it says that function use is invalid

 采纳的回答

David Young
David Young 2012-1-16

0 个投票

The function definition (starting with the word "function") should go into its own m-file, called fun.m in this case. Make sure the m-file is in a folder that is on your MATLAB path, or is the current folder.

1 个评论

Or make the script itself a function, i.e., add e.g.
function vrmsoptim
at the top.

请先登录,再进行评论。

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Get Started with MATLAB 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by