Error in fminsearch (line 200) fv(:,1) = funfcn(x,varargin{:});

7 views (last 30 days)
I'm trying to get an optimised curve fir for my data, but i get the error
fun = @(x)(x(1).*(A).^2 + x(2).*(B).^2 + x(3).*(C).^2 + x(4).*(D).^2 -(E).^2);
x0 = zeros(1,4);
param = fminsearch(fun,x0);
  2 Comments
Tushar Verma
Tushar Verma on 12 Nov 2020
Edited: Tushar Verma on 12 Nov 2020
Sorry , the post got submitted halfway through. A B C D E are vectors of 576x1 each and the full error is :
im trying to fit the data to a curve using : https://se.mathworks.com/help/matlab/math/example-curve-fitting-via-optimization.html
Unable to perform assignment because the size of the left side
is 1-by-1 and the size of the right side is 576-by-1.
Error in fminsearch (line 200)
fv(:,1) = funfcn(x,varargin{:});
Error in anaLiSis (line 263)
param = fminsearch(fun,x0);

Sign in to comment.

Accepted Answer

Ameer Hamza
Ameer Hamza on 12 Nov 2020
Objective function must return a scalar value, whereas, in your case, it returns a 576x1 vector. Following code uses norm() to take l2-norm of 576x1 to return a scalar.
A = rand(576,1); % example values
B = rand(576,1);
C = rand(576,1);
D = rand(576,1);
E = rand(576,1);
fun = @(x) norm((x(1).*(A).^2 + x(2).*(B).^2 + x(3).*(C).^2 + x(4).*(D).^2 -(E).^2));
x0 = zeros(1,4);
param = fminsearch(fun,x0);

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!