lsqcurvefit issues due to variables being several orders of magnitu... (2024)

68 views (last 30 days)

Show older comments

Andrew on 30 Jul 2024 at 19:20

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/2141596-lsqcurvefit-issues-due-to-variables-being-several-orders-of-magnitude-different

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/2141596-lsqcurvefit-issues-due-to-variables-being-several-orders-of-magnitude-different

Edited: Torsten on 31 Jul 2024 at 10:43

  • data.mat
  • V1.mat
  • Vp.mat

Open in MATLAB Online

I am trying to use lsqcurvefit to fit an equation to some data in order to solve for a couple variables. I have included the part of the code below that covers this. I am trying to solve for coeff(1) and coeff(2), The problem is that when I run lsqcurvefit, it is just using whatever my initial guesses are and outputting that as the solution. I suspect it is because my values for the coefficients will be several orders of magnitude different. You can kind of get an idea for this by looking at coeff0. Has anyone else run into this problem and/or do you know how to work around it? Any insight would be greatly appreciated.

load('V1.mat')

load('Vp.mat')

load('data.mat')

V = [V1, Vp]; %voltages

a = 1.1792;

b = 0.5;

e = 1.60217662e-19;

Area = 4.7909e-7;

mi = 39.948./(6.022e23.*1000);

coeff0 = [7e10 4]; %initial guess

qn7 = @(coeff, VV) e^1.5*coeff(1)*Area*sqrt(coeff(2)/(2*pi*mi))*100^3*...

(a*(-VV(:,1)/coeff(2)).^b.*tanh(VV(:,2)/(2*coeff(2))) + ...

(a*(-VV(:,1)/coeff(2)).^b - a*(-(VV(:,1)+VV(:,2))/coeff(2)).^b)./(exp(VV(:,2)/coeff(2))+1));

options = optimoptions('lsqcurvefit','Algorithm','levenberg-marquardt','OptimalityTolerance',1e-16,'FunctionTolerance',1e-16);

lb = [];

ub = [];

Unrecognized function or variable 'Ip'.

[vals, resnorm, out, flag] = lsqcurvefit(qn7, coeff0, V, data(:,2),lb,ub);

plot(data(:,1),data(:,2),'x',data(:,1),qn7(vals,V),'b-')

5 Comments

Show 3 older commentsHide 3 older comments

Torsten on 30 Jul 2024 at 21:09

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2141596-lsqcurvefit-issues-due-to-variables-being-several-orders-of-magnitude-different#comment_3224706

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2141596-lsqcurvefit-issues-due-to-variables-being-several-orders-of-magnitude-different#comment_3224706

Edited: Torsten on 30 Jul 2024 at 21:14

Andrew on 30 Jul 2024 at 21:13

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2141596-lsqcurvefit-issues-due-to-variables-being-several-orders-of-magnitude-different#comment_3224721

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2141596-lsqcurvefit-issues-due-to-variables-being-several-orders-of-magnitude-different#comment_3224721

Edited: Andrew on 30 Jul 2024 at 21:40

Sorry, I thought I changed that. Ip(:,point) is just data(:,2)

Edit: I missed some stuff when I copied and pasted it over. I just fixed it. I clicked the MATLAB online tab and it ran.

dpb on 30 Jul 2024 at 23:41

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2141596-lsqcurvefit-issues-due-to-variables-being-several-orders-of-magnitude-different#comment_3224756

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2141596-lsqcurvefit-issues-due-to-variables-being-several-orders-of-magnitude-different#comment_3224756

Open in MATLAB Online

  • data.mat
  • V1.mat
  • Vp.mat

load('V1.mat')

load('Vp.mat')

load('data.mat')

V = [V1, Vp]; %voltages

a = 1.1792;

b = 0.5;

e = 1.60217662e-19;

Area = 4.7909e-7;

mi = 39.948./(6.022e23.*1000);

coeff0 = [6E10 9]; %initial guess

Eqn7 = @(coeff, VV) e^1.5*coeff(1)*Area*sqrt(coeff(2)/(2*pi*mi))*100^3*...

(a*(-VV(:,1)/coeff(2)).^b.*tanh(VV(:,2)/(2*coeff(2))) + ...

(a*(-VV(:,1)/coeff(2)).^b - a*(-(VV(:,1)+VV(:,2))/coeff(2)).^b)./(exp(VV(:,2)/coeff(2))+1));

options = optimoptions('lsqcurvefit','Algorithm','levenberg-marquardt','OptimalityTolerance',1e-16,'FunctionTolerance',1e-16);

lb = [];

ub = [];

[vals, resnorm, out, flag] = lsqcurvefit(Eqn7, coeff0, V, data(:,2),lb,ub);

Initial point is a local minimum.Optimization completed because the size of the gradient at the initial point is less than the value of the optimality tolerance.

plot(data(:,1),data(:,2),'x',data(:,1),Eqn7(vals,V),'b-')

lsqcurvefit issues due to variables being several orders of magnitu... (5)

I ran out of time at the moment, but no matter what I tried for initial guesses, the gradient was calculated to be essentially zero so it thinks it's converged no matter the input guess.

I changed parameters greatly to see the influence each has on the result and purely by trial and error found the above that aren't too terrible.

You may want to investigate the gradient calculations more thoroughly and see what might be going on there...

Andrew on 31 Jul 2024 at 0:50

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2141596-lsqcurvefit-issues-due-to-variables-being-several-orders-of-magnitude-different#comment_3224786

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2141596-lsqcurvefit-issues-due-to-variables-being-several-orders-of-magnitude-different#comment_3224786

Thank you for the help. How do I look at the gradient calculations?

This is actually one of hundreds of points I need to look at so I need to figure out how to get it to work without me manually finding the best values.

Andrew on 31 Jul 2024 at 4:34

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2141596-lsqcurvefit-issues-due-to-variables-being-several-orders-of-magnitude-different#comment_3224901

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2141596-lsqcurvefit-issues-due-to-variables-being-several-orders-of-magnitude-different#comment_3224901

Edited: Andrew on 31 Jul 2024 at 4:55

Open in MATLAB Online

So I tried scaling the variables and even the equation to see if it did have to do with my variables being to different in terms of order of magnitude. That didn't do anything.

I also tried using the trust-region-reflective algorithm. It didn't improve the fit most of the time. Interesting enough though, I have some data points that were near the edge of my plasma where my densities are pretty low and I didn't get the signature "S" shape I was looking for. Trust-region-reflective did tend to work better for those cases.

I ended messing around with the options and the bounds. The following options and bounds get me a pretty good fit for the majority of my data points. I realized my OptimalityTolerance and FunctionTolerance were causing it to step out. I cranked those down and then it started having issues with the StepTolerance the majority of the time and on occasion the ConstraintTolerance. It appears that the majority of the time, my stepsize is getting extremely small and causing it to step out. I need to look in to how the step size is calculated and figure out why this would be happening when the fit is still pretty far off. Also, my tolerances are probably overkill right now. I was just cranking them down to see what happens, but after a certain point it didn't really make a difference.

options = optimoptions('lsqcurvefit','Algorithm','levenberg-marquardt','OptimalityTolerance',1e-100,'FunctionTolerance',1e-100,'StepTolerance',1e-100,'ConstraintTolerance',1e-100);

lb = [1e8 0];

ub = [1e13 20];

Edit: Adding the following option to the options appears to make it converge succesfully more often.

'ScaleProblem','jacobian'

Sign in to comment.

Sign in to answer this question.

Answers (1)

Torsten on 31 Jul 2024 at 10:37

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2141596-lsqcurvefit-issues-due-to-variables-being-several-orders-of-magnitude-different#answer_1492866

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2141596-lsqcurvefit-issues-due-to-variables-being-several-orders-of-magnitude-different#answer_1492866

Edited: Torsten on 31 Jul 2024 at 10:43

Open in MATLAB Online

  • Vp.mat
  • V1.mat
  • data.mat

load('V1.mat')

load('Vp.mat')

load('data.mat')

V = [V1, Vp]; %voltages

a = 1.1792;

b = 0.5;

e = 1.60217662e-19;

Area = 4.7909e-7;

mi = 39.948./(6.022e23.*1000);

coeff0 = [1e-5 10]; %initial guess

Eqn7 = @(coeff, VV) coeff(1)*sqrt(coeff(2))*...

(a*(-VV(:,1)/coeff(2)).^b.*tanh(VV(:,2)/(2*coeff(2))) + ...

(a*(-VV(:,1)/coeff(2)).^b - a*(-(VV(:,1)+VV(:,2))/coeff(2)).^b)./(exp(VV(:,2)/coeff(2))+1));

%options = optimoptions('lsqcurvefit','Algorithm','levenberg-marquardt','OptimalityTolerance',1e-16,'FunctionTolerance',1e-16);

lb = [];

ub = [];

[vals, resnorm, out, flag] = lsqcurvefit(Eqn7, coeff0, V, data(:,2));

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.

plot(data(:,1),data(:,2),'x',data(:,1),Eqn7(vals,V),'b-')

lsqcurvefit issues due to variables being several orders of magnitu... (9)

format long

vals(1)

ans =

2.620938226982966e-06

vals(1)=vals(1)*sqrt(2*pi*mi)/(e^1.5*Area*100^3);

vals(1)

ans =

5.507330928625227e+10

vals(2)

ans =

9.902502214206862

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

Mathematics and OptimizationSymbolic Math ToolboxMuPADMuPAD Language FundamentalsData TypesData StructuresCommon Operations

Find more on Common Operations in Help Center and File Exchange

Tags

  • curve fitting

Products

  • MATLAB

Release

R2023a

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


lsqcurvefit issues due to variables being several orders of magnitu... (10)

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)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

lsqcurvefit issues due to variables being several orders of magnitu... (2024)
Top Articles
Medical directors weigh in on Akorn’s voluntary medication recall
Healthy Benefits Plus.com/Anthembcbsotc 2022
Jail Inquiry | Polk County Sheriff's Office
Where are the Best Boxing Gyms in the UK? - JD Sports
Koopa Wrapper 1 Point 0
Lakers Game Summary
Part time Jobs in El Paso; Texas that pay $15, $25, $30, $40, $50, $60 an hour online
Chatiw.ib
Top Scorers Transfermarkt
The Potter Enterprise from Coudersport, Pennsylvania
Owatc Canvas
Mylife Cvs Login
Acbl Homeport
Craigslist Labor Gigs Albuquerque
Diablo 3 Metascore
Hijab Hookup Trendy
Classic Lotto Payout Calculator
Grasons Estate Sales Tucson
Truck Trader Pennsylvania
Iu Spring Break 2024
Persona 5 Royal Fusion Calculator (Fusion list with guide)
Craigslist Pearl Ms
Craigslist St. Cloud Minnesota
Minnick Funeral Home West Point Nebraska
PCM.daily - Discussion Forum: Classique du Grand Duché
Craigslist Maryland Trucks - By Owner
Craigslist Roseburg Oregon Free Stuff
Cpt 90677 Reimbursem*nt 2023
Costco Jobs San Diego
Copper Pint Chaska
Taylored Services Hardeeville Sc
Vlacs Maestro Login
Earthy Fuel Crossword
Tire Pro Candler
3 Bedroom 1 Bath House For Sale
Jambus - Definition, Beispiele, Merkmale, Wirkung
Www Violationinfo Com Login New Orleans
Free Robux Without Downloading Apps
Shoreone Insurance A.m. Best Rating
Studio 22 Nashville Review
9781644854013
Myql Loan Login
Tirage Rapid Georgia
Bunkr Public Albums
Carteret County Busted Paper
Ethan Cutkosky co*ck
Kenner And Stevens Funeral Home
Funkin' on the Heights
Online TikTok Voice Generator | Accurate & Realistic
Marine Forecast Sandy Hook To Manasquan Inlet
Cars & Trucks near Old Forge, PA - craigslist
Naughty Natt Farting
Latest Posts
Article information

Author: Duncan Muller

Last Updated:

Views: 6100

Rating: 4.9 / 5 (59 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Duncan Muller

Birthday: 1997-01-13

Address: Apt. 505 914 Phillip Crossroad, O'Konborough, NV 62411

Phone: +8555305800947

Job: Construction Agent

Hobby: Shopping, Table tennis, Snowboarding, Rafting, Motor sports, Homebrewing, Taxidermy

Introduction: My name is Duncan Muller, I am a enchanting, good, gentle, modern, tasty, nice, elegant person who loves writing and wants to share my knowledge and understanding with you.