lsqcurvefit - Error using Data_fit>@​(x)myfun2(​x,xdata) Too many input arguments.

2 visualizaciones (últimos 30 días)
Hello,
I am having this error in the title running a lsqcurvefit program. I'm not very proficient in MATLAB and I think I'm making some simple mistakes which I just could not figure out. I checked similar questions asked before and consulted documentation but still culd not figure out. Hence this plea for help.
I am trying to do a least sq fit for an implicit function, i.,e, I need to solve an equation with a given set of optimizing parameters x to get the function that is being optimized. I'm doing a fit also with another function that is explicit and that is working fine.
Here is the code.
I would appreciate help anyone can provide.
Thank you.
Sid
P.S. There are some leftover codes left from trying various things like N not being passed in the E_fun and putting a dimension statement for y in the E_fun, etc.
------------
div1=5.4/6.4;
N=11;
B_max=10.0;
xdata=zeros(1,N);
x_D=zeros(1,N);
g=zeros(1,N);
beta=zeros(1,N);
x=zeros(1,2);
ydata = [6.4, 6.5, 6.8, 7.1, 7.5, 8.0, 9.0, 10.0, 11.0, 11.7, 12.5]; %in units of inch divs
for i=1:11
B=(i-1)*1.0;
xdata(i)=B;
ydata(i)=div1*ydata(i);
end
fun_DF = @(x,xdata)x(1).*(1+0.25.*((1.73.*xdata./x(1)).^2)).^0.5;
x0=(5.4);
lb=4.0;
ub=6.0;
[x,resnorm,residual,exitflag,output] = lsqcurvefit(fun_DF,x0,xdata,ydata,lb,ub);
fprintf('\nFD eps0= %.3f\t resnorm= %.3f\n',x, resnorm);
times = linspace(xdata(1),xdata(end));
plot(xdata,ydata,'ko',times,fun_DF(x,times),'b-')
%Parabolic potentail
x0=[5.0, 0.1];
lb=[4.0, 0.01];
ub=[6.0, 1.0];
myfun2 = @(x,xdata) E_fn(x,xdata); % parameterized function
fun2 = @(x) myfun2(x,xdata); % function of x alone; x(1)=eps0, x(2)=alpha0
[x,resnorm,residual,exitflag,output] = lsqcurvefit(fun2,x0,xdata,ydata,lb,ub);
fprintf('\nexp eps0= %.3f alpha0= %.3f\t resnorm= %.3f',x, resnorm);
function output = E_fn(x,xdata)
N=11;
y=zeros(1,N);
eps0=x(1);
alpha0=x(2);
beta=11.75*alpha0*alpha0/eps0;
for i=1:N
g=(1.0+0.25*(1.73*xdata(i)/eps0)^2)^0.5;
myfun3 = @(xd, beta, g) x_D_fn(xd, beta, g); % parameterized function
fun3 = @(xd) myfun3(xd, beta, g); % function of x alone
[theroot, fval, exitflag, output] = fzero(fun3, 0.7);
x_D = theroot;
f=(1.0+(g*g-1)/(g*x_D*x_D)^2)*g*x_D*x_D+(0.5/beta)*(1-1/(1+2*beta/(g*x_D*x_D)));
y(i)=0.5*eps0*f;
end
output=y;
end
function y=x_D_fn(x,beta,g)
y=x^4-1/(g*(1+2*beta/(g*x*x)))^2-(g*g-1)/(g*g);
end

Respuesta aceptada

Matt J
Matt J el 13 de Jun. de 2020
Editada: Matt J el 13 de Jun. de 2020
The second call to lsqcurvefit should be,
___ = lsqcurvefit(@E_fn,x0,xdata,ydata,lb,ub);
or
___ = lsqcurvefit(myfun2,x0,xdata,ydata,lb,ub);
  3 comentarios
Matt J
Matt J el 13 de Jun. de 2020
Like the error message says ... one of your x,y data pairs can't be plotted because they are not the same length.
Sid Chaudhuri
Sid Chaudhuri el 13 de Jun. de 2020
Yup. Now I see that my E_fun is hardcoded at N=11 because I couldn't pass the N parameter. I tried to cange the E_fun def line to "myfun2 = @(xe,xdata,N) E_fn(xe,xdata,N); and correspondingly to the function E_fun. That did not work. How could I pass N to E_fun function?
Thanks a lot.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Solver Outputs and Iterative Display en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by