optimalization of non linear function, Inputs must be a scalar and a square matrix

1 view (last 30 days)
Dear Matlab users, I'm a newbie in Matlab, and try to solve follewing problem.
I try to optimize function for data :
I need to use simplex method to find parameter a,b,c, and r (in script (p1)... (p4))
The problem is that I use wrong bracket or something similar.
format compact
format long
xdata = [0.00,0.20,0.40,0.60,0.80,1.00,1.20,1.40,1.60,1.80,2.00,2.20,... 2.40,2.60,2.80,3.00,3.20,3.40,3.60,3.80,4.00,4.20,4.40,4.60,4.80,5.00];
ydata = [0.007,0.041,0.165,0.449,0.816,0.982,0.741,0.212,-0.362,-0.808,-0.975,... -0.774,-0.290,0.290,0.775,0.982,0.849,0.527,0.237,0.077,0.018,0.003,0.000,0.000,0.000,0.000];
%Function to calculate the sum of residuals for our a given parameters
fun = @(p) sum((ydata - (-1*(p(1)-p(2)*((xdata)-p(3)).^4)*exp(-p(4)*((xdata)-p(3).^2)))).^2) %starting guess for our parameters pguess = [1.0,12.0,1.03,2.4];
%optimise [p,fminres] = fminsearch(fun,pguess)
Does anybody know how to around this?
Thank you
P.

Answers (1)

Alan Weiss
Alan Weiss on 5 Aug 2014
It would be easier to read your code if you would use the {}Code button to format all of it. And it would be easier to diagnose your problem if you would give us the error message that MATLAB issues.
That said, it seems to me that you forgot a . in the .* by the exponential term:
...p(3)).^4)*exp(-p(4)*((...
should be
...p(3)).^4).*exp(-p(4)*((...
Alan Weiss
MATLAB mathematical toolbox documentation
  1 Comment
Pavol Namer
Pavol Namer on 7 Aug 2014
Edited: Pavol Namer on 7 Aug 2014
Thank you for your answer. So there is my code:
format compact
format long
x = [0.000,0.200,0.400,0.600,0.800,1.000,1.200,1.400,1.600,1.800,2.000,2.200,2.400,2.600,2.800,3.000,3.200,3.400,3.600,3.800,4.000,4.200,4.400,4.600,4.800,5.000]';
y = [0.007,0.041,0.165,0.449,0.816,0.982,0.741,0.212,-0.362,-0.808,-0.975,-0.774,-0.290,0.290,0.775,0.982,0.849,0.527,0.237,0.077,0.018,0.003,0.000,0.000,0.000,0.000];
%Function to calculate the sum of residuals for our a given parameters
fun= @(p) sum((y-(-1*(p(1)-p(2)*(((x)-p(3)).^4)).*(exp(-1*p(4)*((x)-p(3)).^2)))).^2)
%starting guess for our parameters
pguess = [0.9787,12.548,1.128,2.855];
%optimise
[p,fminres] = fminsearch(fun,pguess)
Now I've got several problems.
1) there is a following error message:
_Error in fminsearch (line 190)
fv(:,1) = funfcn(x,varargin{:});
Error in script_optimise_function (line 14) [p,fminres] = fminsearch(fun,pguess)
2.) the equation of function "fun" is incorrect. It need to be:
And it need to have this shape of XY dependence:
But it does totaly wrong shape if I try to plot this function in matlab.
Please If you know what's wrong, let me know.
Thanks for your willingness.
PN

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!