Need help with converting Mathematica code to MATLAB code
5 ビュー (過去 30 日間)
古いコメントを表示
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1054360/image.jpeg)
And the curves look like the below after minimizing the different between a curve and the target curve.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1054365/image.jpeg)
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 件のコメント
回答 (1 件)
Aditya
2023 年 10 月 9 日
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.
You can also use ToMatlab package built for Mathematica. Please refer to the following link for more information:
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 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!