Minimize using fminsearch?

조회 수: 6 (최근 30일)
Brady Retzlaff
Brady Retzlaff 2015년 11월 3일
편집: Geoff Hayes 2015년 11월 4일
The question is this
Use fminsearch to minimize the function you wrote above, using an initial guess of your choosing. Use the resulting c1 and c2 to evaluate f(x) at the values of x from exercise 2, storing the result as y. Plot the data and your curve (using x and y) to verify the fit. Once verified, save the first 10 elements of y
My code is:
objFunc = @(c) ( errFunc(c) );
C = fminsearch(objFunc, [1.0;1.0]);
for j=1:n
y(j) = C(1)*sin(x(j)+C(2));
end
What am i doing wrong??? i'm sure my errFunc is correct...
  댓글 수: 3
Brady Retzlaff
Brady Retzlaff 2015년 11월 4일
편집: Geoff Hayes 2015년 11월 4일
if true
% code
end
my effFunc is as follows...
function [e] = errFunc(c)
data2x =
data2y =
e = 0.0;
M=length(data2x);
for j=1:M
e = e + (c(1)*sin(data2x(j)+c(2)) - data2y(j))^2;
end
end
I know that it's incorrect because when i plot it next to the data im trying to fit, the graph is not even close... but im not getting an error
Star Strider
Star Strider 2015년 11월 4일
You need to use fminsearch to minimise the sum of squares difference between your objective function (which is a function only of your parameters and your independent variable) and your dependent variable. I usually do this with two functions, the objective function and the sum-of-squares cost function, and minimise the cost function.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by