MultiStart Using lsqcurvefit or lsqnonlin - MATLAB & Simulink (2024)

Open Live Script

This example shows how to fit a function to data using lsqcurvefit together with MultiStart. The end of the example shows the same solution using lsqnonlin.

Many fitting problems have multiple local solutions. MultiStart can help find the global solution, meaning the best fit. This example first uses lsqcurvefit because of its convenient syntax.

The model is

y=a+bx1sin(cx2+d),

where the input data is x=(x1,x2), and the parameters a, b, c, and d are the unknown model coefficients.

Step 1. Create the objective function.

Write an anonymous function that takes a data matrix xdata with N rows and two columns, and returns a response vector with N rows. The function also takes a coefficient matrix p, corresponding to the coefficient vector (a,b,c,d).

fitfcn = @(p,xdata)p(1) + p(2)*xdata(:,1).*sin(p(3)*xdata(:,2)+p(4));

Step 2. Create the training data.

Create 200 data points and responses. Use the values a=-3,b=1/4,c=1/2,d=1. Include random noise in the response.

rng default % For reproducibilityN = 200; % Number of data pointspreal = [-3,1/4,1/2,1]; % Real coefficientsxdata = 5*rand(N,2); % Data pointsydata = fitfcn(preal,xdata) + 0.1*randn(N,1); % Response data with noise

Step 3. Set bounds and initial point.

Set bounds for lsqcurvefit. There is no reason for d to exceed π in absolute value, because the sine function takes values in its full range over any interval of width 2π. Assume that the coefficient c must be smaller than 20 in absolute value, because allowing a high frequency can cause unstable responses or inaccurate convergence.

lb = [-Inf,-Inf,-20,-pi];ub = [Inf,Inf,20,pi];

Set the initial point arbitrarily to (5,5,5,0).

p0 = 5*ones(1,4); % Arbitrary initial pointp0(4) = 0; % Ensure the initial point satisfies the bounds

Step 4. Find the best local fit.

Fit the parameters to the data, starting at p0.

Local minimum possible.lsqcurvefit stopped because the final change in the sum of squares relative to its initial value is less than the value of the function tolerance.
xfitted = 1×4 -2.6149 -0.0238 6.0191 -1.6998
errorfitted = 28.2524

lsqcurvefit finds a local solution that is not particularly close to the model parameter values (–3,1/4,1/2,1).

Step 5. Set up the problem for MultiStart.

Create a problem structure so MultiStart can solve the same problem.

problem = createOptimProblem('lsqcurvefit','x0',p0,'objective',fitfcn,... 'lb',lb,'ub',ub,'xdata',xdata,'ydata',ydata);

Step 6. Find a global solution.

Solve the fitting problem using MultiStart with 50 iterations. Plot the smallest error as the number of MultiStart iterations.

ms = MultiStart('PlotFcns',@gsplotbestf);[xmulti,errormulti] = run(ms,problem,50)

MultiStart Using lsqcurvefit or lsqnonlin- MATLAB & Simulink (1)

MultiStart completed the runs from all start points. All 50 local solver runs converged with a positive local solver exitflag.
xmulti = 1×4 -2.9852 -0.2472 -0.4968 -1.0438
errormulti = 1.6464

MultiStart finds a global solution near the parameter values (–3,–1/4,–1/2,–1). (This is equivalent to a solution near preal = (–3,1/4,1/2,1), because changing the sign of all the coefficients except the first gives the same numerical values of fitfcn.) The norm of the residual error decreases from about 28 to about 1.6, a decrease of more than a factor of 10.

Formulate Problem for lsqnonlin

For an alternative approach, use lsqnonlin as the fitting function. In this case, use the difference between predicted values and actual data values as the objective function.

fitfcn2 = @(p)fitfcn(p,xdata)-ydata;[xlsqnonlin,errorlsqnonlin] = lsqnonlin(fitfcn2,p0,lb,ub)
Local minimum possible.lsqnonlin stopped because the final change in the sum of squares relative to its initial value is less than the value of the function tolerance.
xlsqnonlin = 1×4 -2.6149 -0.0238 6.0191 -1.6998
errorlsqnonlin = 28.2524

Starting from the same initial point p0, lsqnonlin finds the same relatively poor solution as lsqcurvefit.

Run MultiStart using lsqnonlin as the local solver.

problem2 = createOptimProblem('lsqnonlin','x0',p0,'objective',fitfcn2,... 'lb',lb,'ub',ub');[xmultinonlin,errormultinonlin] = run(ms,problem2,50)

MultiStart Using lsqcurvefit or lsqnonlin- MATLAB & Simulink (2)

MultiStart completed the runs from all start points. All 50 local solver runs converged with a positive local solver exitflag.
xmultinonlin = 1×4 -2.9852 -0.2472 -0.4968 -1.0438
errormultinonlin = 1.6464

Again, MultiStart finds a much better solution than the local solver alone.

Related Topics

  • Visualize the Basins of Attraction
  • Find Global or Multiple Local Minima

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

MultiStart Using lsqcurvefit or lsqnonlin- MATLAB & Simulink (3)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

MultiStart Using lsqcurvefit or lsqnonlin
- MATLAB & Simulink (2024)

FAQs

What is the difference between MultiStart and GlobalSearch in Matlab? ›

The main differences between GlobalSearch and MultiStart are: GlobalSearch uses a scatter-search mechanism for generating start points. MultiStart uses uniformly distributed start points within bounds, or user-supplied start points.

What does lsqcurvefit do in Matlab? ›

lsqcurvefit passes x to your objective function in the shape of the x0 argument. For example, if x0 is a 5-by-3 array, then lsqcurvefit passes x to fun as a 5-by-3 array. then the function fun must return a second output argument with the Jacobian value J (a matrix) at x .

What is the difference between Fitlm and fit in MATLAB? ›

_ *fitlm* _ belongs to the Statistics toolbox and is used for linear regression. _ *fit* _ belongs to the Curve-fitting toolbox and is used to fit data to a curve or a surface.

What is the difference between ValueChangedFcn and ValueChangingFcn in MATLAB? ›

For example, sliders have two callback properties: ValueChangedFcn and ValueChangingFcn . The ValueChangedFcn callback executes after the user moves the slider and releases the mouse. The ValueChangingFcn callback for the same component executes repeatedly while the user moves the slider.

What is lsqnonlin in MATLAB? ›

Solve nonlinear least-squares (nonlinear data-fitting) problems.

What is the difference between Lsqnonlin and Fmincon? ›

fmincon minimizes the sum of squares given as ∑ i F i 2 , where F is a vector function. In contrast, lsqnonlin works with the entire vector F , meaning it has access to all the components of the sum. In other words, fmincon can access only the value of the sum, but lsqnonlin can access each component separately.

What is least square fitting? ›

The least squares method is a mathematical technique that allows the analyst to determine the best way of fitting a curve on top of a chart of data points. It is widely used to make scatter plots easier to interpret and is associated with regression analysis.

What are the two types of loops in MATLAB? ›

MATLAB provides different types of loops to handle looping requirements, including:
  • While loops.
  • For loops.
  • Nested loops.
Mar 9, 2023

What are the different types of stateflow in MATLAB? ›

Types of Coverage for Stateflow Charts
  • Decision Coverage for Stateflow Charts. ...
  • Condition Coverage for Stateflow Charts. ...
  • MCDC Coverage for Stateflow Charts. ...
  • Relational Boundary Coverage for Stateflow Charts. ...
  • Simulink Design Verifier Coverage for Stateflow Charts.

What is difference between .m and .mlx file in MATLAB? ›

MLX files are stored in Live Script format instead of plain text. MLX files are displayed with code in the Live Editor and you can add and view formatted text in the Live Editor. M files are displayed in the command window and must be published to view.

What is the difference between Pzmap and Pzplot in MATLAB? ›

pzplot lets you plot pole-zero maps with a broader range of plot customization options than pzmap . You can use pzplot to obtain the plot handle and use it to customize the plot, such as modify the axes labels, limits and units.

References

Top Articles
Cranberry Curd Tart Recipe
45 Recipes We Stole from Our Sisters
Car Parts Open Now
Main Moon Ashland Ohio Menu
Chris Wragge Illness
Louisville Kentucky Craigslist Cars And Trucks By Owner
Babylon Showtimes Near Airport Stadium 12
Abc Order Hs Login
Does Publix Pharmacy Accept Sunshine Health
Relic Gate Nms
Wausau Pilot Obituaries
Officially Announcing: Skyward
Schmidt & Schulta Funeral Home Obituaries
Pollen Count In Brandon Fl
Localhotguy
Katmoie
C And B Tracy
Skyward Weatherford Isd Login
T33N Leaks 5 17
Th 8 Best Army
Yellow Kitchen Curtains Walmart
Starfield PC, XSX | GRYOnline.pl
Math Nation Algebra 2 Practice Book Answer Key
Live Stream Portal
Altametrics Login Little Caesars
Clean My Mac Sign In
Magicseaweed Bob Hall
Apple iPhone SE 2nd Gen (2020) 128GB 4G (Very Good- Pre-Owned)
Conner Westbury Funeral Home Griffin Ga Obituaries
Amerikaanse dollar bestellen | USD kopen
Ethos West Mifflin
Poskes Parts
1084 Sadie Ridge Road, Clermont, FL 34715 - MLS# O6240905 - Coldwell Banker
De Chromecast met Google TV en stembediening instellen
Surprise | Visit Arizona
Shiawassee County 911 Active Events
Codex - Chaos Space Marines 9th Ed (Solo Reglas) - PDFCOFFEE.COM
Entourage Yearbook Login
Understanding Turbidity, TDS, and TSS
Meat Grinders At Menards
Bulk Amateur 51 Girls Statewins Leak – BASL058
Craigslist Creative Gigs
Best Pizza In Ft Myers
Feetfinder Reviews Trustpilot
Nailery Open Near Me
Craigslist For Pets For Sale
Ucla Football 247
Acadis Portal Missouri
Hotels Near William Woollett Jr Aquatics Center
Four Embarcadero Center - Lot #77
Watch It Horror Thriller movies | Crystal panel
Potion To Reset Attributes Conan
Latest Posts
Article information

Author: Eusebia Nader

Last Updated:

Views: 6361

Rating: 5 / 5 (60 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Eusebia Nader

Birthday: 1994-11-11

Address: Apt. 721 977 Ebert Meadows, Jereville, GA 73618-6603

Phone: +2316203969400

Job: International Farming Consultant

Hobby: Reading, Photography, Shooting, Singing, Magic, Kayaking, Mushroom hunting

Introduction: My name is Eusebia Nader, I am a encouraging, brainy, lively, nice, famous, healthy, clever person who loves writing and wants to share my knowledge and understanding with you.