Need help with converting Mathematica code to MATLAB code

2 views (last 30 days)
I have a dataset with values of multiple curves. An example plot is shown below. I want to scale the curves (up/down) so that all curves overlap or get closer to the target curve. The following is a sample dataset and
And the curves look like the below after minimizing the different between a curve and the target curve.
The following is the Mathematica code with generates the output in the second figure. (code and mathematical functions posted here https://scicomp.stackexchange.com/questions/41552/minimize-distance-between-curves)
data = {{{157, 60}, {140, 57}, {121, 52}, {103, 47}, {86, 39}, {67, 28}, {50,21}, {32, 15}}, {{159, 70}, {140, 67}, {122, 63}, {105, 58}, {85, 45}, {68, 42}, {50, 34}, {33, 29}}, {{157, 74}, {140, 71}, {121, 70}, {105, 65}, {86, 55}, {69, 52}, {52, 43}, {32, 37}}, {{158, 100}, {141, 100}, {123, 95}, {105, 90}, {88, 78}, {69, 72}, {51, 57}, {33, 46}}, {{160, 143}, {140, 146}, {123, 146}, {105, 145}, {86, 136}, {70, 121}, {51, 97}, {34, 60}}}
gr1 = ListPlot[data, AspectRatio -> 1.3];
gr2 = ListLinePlot[data, AspectRatio -> 1.3];
Show[gr1, gr2]
min = 1000000;
For[r = 1, r <= 5, r++,
obj = Total[Table[If[k != r, Sum[(data[[k]][[j, 2]] a[k] + b[k] - data[[r]][[j, 2]])^2, {j, 1, 8}], 0], {k, 1, 5}]];
vars = Flatten[Table[If[k != r, {a[k], b[k]}], {k, 1, 5}]];
sol = Minimize[obj, vars];
If[sol[[1]] < min, rmin = r; min = sol[[1]]; coefs = sol[[2]]]
]
sdata = Table[Table[{data[[k]][[j, 1]], data[[k]][[j, 2]] a[k] + b[k]} /. coefs, {j, 1, 8}], {k, 1, 5}] /. {a[rmin] -> 1, b[rmin] -> 0}
gr1b = ListPlot[sdata, AspectRatio -> 1.3];
gr2b = ListLinePlot[sdata, AspectRatio -> 1.3];
Show[gr1b, gr2b]
I would like to ask for help in converting this to MATLAB code.
  2 Comments
Deepa Maheshvare
Deepa Maheshvare on 4 Jul 2022
I want to integrate with othe functions that are available in MATLAB

Sign in to comment.

Answers (0)

Categories

Find more on Curve Fitting Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by