# Need help with converting Mathematica code to MATLAB code

5 vues (au cours des 30 derniers jours)
Deepa Maheshvare le 4 Juil 2022
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 commentairesAfficher AucuneMasquer Aucune
Sam Chak le 4 Juil 2022
Just wondering. Why did you decide to switch from Mathematica to MATLAB?
Deepa Maheshvare le 4 Juil 2022
I want to integrate with othe functions that are available in MATLAB

Connectez-vous pour commenter.

### Réponses (1)

Hi Deepa,
From your query, I understand that you would like to convert a Mathematica program to MATLAB script. As of now MATLAB does not have any direct interface with Mathematica. However, there is a user-contributed file available on MATLAB Central at the following URL:
This file is not owned by MathWorks. If you have any questions or encounter any issues, kindly reach out to the author for assistance.
Here is an example for the same:
Place the file in applications folder of your base directory.
<< ToMatlab
some_expression ToMatlab
Hope this helps!
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Get Started with Optimization Toolbox dans Help Center et File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by