Use Manipulate function in Mathematica to fit function to data - wolfram-mathematica

I want to use the Manipulate function in Mathematica to fit an analytical function to a set of (x,y) data. I want to plot the dataset on the same axes that I use to manipulate the function (so I can get a visual check of how manipulating the parameters improves the fit, but I cannot find the correct syntax to draw the points behind the manipulated curve. Any solutions to this? Many thanks!

Show[plot1,plot2,...] will overlay the plots, see the docs on Show.
In[1]:= data = Table[{x, x^2+2*x+RandomReal[{-.1,.1}]}, {x,-3,3}];
Manipulate[
Show[ListPlot[data], Plot[a*x^2 + b*x + c, {x, -3, 3}]],
{{a, 0}, -4, 4}, {{b, 0}, -4, 4}, {{c, 2}, -4, 4}]
Out[1]= ...PlotSnipped...

Related

Simple programming techniques / tricks in Mathematica for making graphics for the math book? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I tried to use it. And it's really nice for some Plots, but when its about making for example a triangle I found it quite complicated. I figured out how to draw a triangle but how to add that angle marks, those curved lines?
And since I'm beginner into this job, of writing a book, can anyone recommend me which is the best way to accomplish good looking graphics, for example as in the picture below. Which programs are best to use.
Thanks for any suggestions and recommendations.
Here is a simple/basic way to do the first one:
Graphics[{
(* The dashed circle segment *)
{
Dashing[{.04, .01}],
Darker[Orange],
AbsoluteThickness[2],
Circle[{0, 0}, 1, {1, 2 \[Pi]}]
},
(* The solid circle segment *)
{
Orange,
AbsoluteThickness[2],
Circle[{0, 0}, 1, {0, 1}]
},
(* The radial lines and the small circle segment *)
Line[{{0, 0}, {1, 0}}],
Line[{{0, 0}, {Cos[1], Sin[1]}}],
Circle[{0, 0}, .2, {0, 1}],
(* Various text labels *)
{
Text[Style["\[Theta]", 24], .3 {Cos[.5], Sin[.5]}],
Text[Style["s", 24], 1.1 {Cos[.5], Sin[.5]}],
Text[Style["r", 24], {.5, -.1}]
}
}]
The following is the exact same thing, but wrapped in Manipulate and parameterized
on the angle alpha:
Manipulate[
Graphics[{
{Dashing[{.04, .01}], Darker[Orange], AbsoluteThickness[2],
Circle[{0, 0}, 1, {\[Alpha], 2 \[Pi]}]},
{Orange, AbsoluteThickness[2], Circle[{0, 0}, 1, {0, \[Alpha]}]},
Line[{{0, 0}, {1, 0}}],
Line[{{0, 0}, {Cos[\[Alpha]], Sin[\[Alpha]]}}],
Circle[{0, 0}, .2, {0, \[Alpha]}],
{Text[Style["\[Theta]",
24], .3 {Cos[\[Alpha]/2], Sin[\[Alpha]/2]}],
Text[Style["s", 24], 1.1 {Cos[\[Alpha]/2], Sin[\[Alpha]/2]}],
Text[Style["r", 24], {.5, -.1}]}
}],
{{\[Alpha], 1}, 0, 2 \[Pi]}]
If you move the slider, the content will change accordingly:
Edit You can get inspiration from the Demonstrations project too. These are the triangle-related demonstrations. After taking a quick look, I think you should see the geometry-related demonstrations by Jay Warendorff. He has made a lot of these, and they use a structured set of geometry-related functions that you most likely can reuse.
Here's an angleArc function to get you started. This is just a small example of a helper function you could use, there's a lot of room for improvement.
angleArc[Polygon[vertices_List, ___], r_, i_] :=
Module[{a, b, c, phi1, phi2},
{a, b, c} = Take[RotateLeft[vertices, i-2], 3];
{phi1, phi2} = Sort#N[{ArcTan ## (a - b), ArcTan ## (c - b)}];
If[phi2 - phi1 > Pi, phi1 += 2 Pi];
Circle[b, r, {phi2, phi1}]
]
poly = Polygon[{{0, 0}, {1, 2}, {2, 1}}];
Graphics[{EdgeForm[Thick], FaceForm[None], poly,
Table[angleArc[poly, .2, i], {i, Length[poly[[1]]]}]}]
Manipulate[
With[{poly = Polygon[{a, b, c}]},
Graphics[
{EdgeForm[Thick], FaceForm[None], poly,
Table[angleArc[poly, .2, i], {i, Length[poly[[1]]]}]},
PlotRange -> 2 {{-1, 1}, {-1, 1}}, Frame -> True
]
],
{{a, {0, 0}}, Locator}, {{b, {1, 0}}, Locator}, {{c, {0, 1}}, Locator}
]
One excellent, Wolfram-supported product is Geometrica. It's not a cheap add-on at $495, but it will produce diagrams like yours far easier than doing them in raw MMA Mathematica. It's basically a very large extension of Szabolcs approach in the comments - a library of functions to draw stuff.
I like Mathematica a lot. But there are better options for geometric drawing.
Here you have a five minutes sketch done with Geometry Expressions (very low resolution used):
Geometry Expressions does some nice planar geometry calculations and can export the results to Mma online.
Yes, Mathematica's built-in graphics tools are a bit quirky in places, and there are some annoying omissions. But there's a good range of basic graphics, and, on the plus side, you're only a few keystrokes away from the mathematical tools you need to rectify some of the deficiencies of the graphics editor. Need an arc? Just calculate it below and copy/paste it in. Can't do that with Adobe Illustrator! There's broad access to equations and greek characters too. It could be helpful when there's nothing else to hand.
Here's the first one - it's not totally unacceptable ... :)
Presentations is an excellent and low-cost ($50) graphics (and much more) package in MMA, written by David Park.
http://home.comcast.net/~djmpark/DrawGraphicsPage.html
It is mentioned as resource n.2 in our stackoverflow tool bag
What is in your Mathematica tool bag?

mathematica Plot with Manipulate shows no output

I was initially attempting visualize a 4 parameter function with Plot3D and Manipulate sliders (with two params controlled by sliders and the other vary in the "x-y" plane). However, I'm not getting any output when my non-plotted parameters are Manipulate controlled?
The following 1d plot example replicates what I'm seeing in the more complex plot attempt:
Clear[g, mu]
g[ x_] = (x Sin[mu])^2
Manipulate[ Plot[ g[x], {x, -10, 10}], {{mu, 1}, 0, 2 \[Pi]}]
Plot[ g[x] /. mu -> 1, {x, -10, 10}]
The Plot with a fixed value of mu has the expected parabolic output in the {0,70} automatically selected plotrange, whereas the Manipulate plot is blank in the {0, 1} range.
I was suspecting that the PlotRange wasn't selected with good defaults when the mu slider control was used, but adding in a PlotRange manually also shows no output:
Manipulate[ Plot[ g[x], {x, -10, 10}, PlotRange -> {0, 70}], {{mu, 1}, 0, 2 \[Pi]}]
This is because the Manipulate parameters are local.
The mu in Manipulate[ Plot[ g[x], {x, -10, 10}], {{mu, 1}, 0, 2 \[Pi]}] is different from the global mu you clear on the previous line.
I suggest using
g[x_, mu_] := (x Sin[mu])^2
Manipulate[Plot[g[x, mu], {x, -10, 10}], {{mu, 1}, 0, 2 \[Pi]}]
The following works too, but it keeps changing the value of a global variable, which may cause surprises later unless you pay attention, so I don't recommend it:
g[x_] := (x Sin[mu])^2
Manipulate[
mu = mu2;
Plot[g[x], {x, -10, 10}],
{{mu2, 1}, 0, 2 \[Pi]}
]
It may happen that you Clear[mu], but find that it gets a value the moment the Manipulate object is scrolled into view.
Another way to overcome Manipulate's localization is to bring the function inside the Manipulate[]:
Manipulate[Module[{x,g},
g[x_]=(x Sin[mu])^2;
Plot[g[x], {x, -10, 10}]], {{mu, 1}, 0, 2 \[Pi]}]
or even
Manipulate[Module[{x,g},
g=(x Sin[mu])^2;
Plot[g, {x, -10, 10}]], {{mu, 1}, 0, 2 \[Pi]}]
Both of which give
Module[{x,g},...] prevents unwanted side-effects from the global context. This enables a simple definition of g: I've had Manipulate[]ed plots with dozens of adjustable parameters, which can be cumbersome when passing all those parameters as arguments to the function.

How to export ContourPlot3D surface and regenerate it in Excel, Originlab or some other similar softwares

I tried this, but failed.
fig3D = ContourPlot3D[ x^2 + y^3 - z^2 == 0, {x, -2, 2}, {y, -2, 2}, {z, -2, 2},
PlotPoints -> 100]
pts = (InputForm#fig3D)[[1, 1, 1]];
ListSurfacePlot3D[pts]
The regenerated surface is very poor. Any suggestions? thanks!
Not too bad if you specify MaxPlotPoints
ListSurfacePlot3D[pts, Mesh -> None, MaxPlotPoints -> 100]
Compare with
ListSurfacePlot3D[pts]
Edit
Regarding the export to Excel, please consider that the Excel surface plot is a very basic construction and requires a matrix whose first file and column are the XY values with the Z values in the inner cells. Example:
So, exporting a working dataset to Excel may require (an unspecified amount of) data massaging.

Dynamic (or forced) scaling of ColorFunction

Reading this question on importing ColorData from matlab, I was wondering if there is a way to change the range of values over which the ColorFunction is scaled. That was probably not entirely clear, so let me show with a figure from matlab (the same example as in the previous question is used)
The plot on the left is the original, with the ColorData mapped to the data values between -1 and 1. Now, I can easily set it to be mapped to the data values between 0 and 1, the result being that all values less than 0 are assigned blue color (lowest in the colormap). PlotRange is the closest function, and using ClippingStyle in addition to that produces a similar figure. However, it doesn't re-scale the ColorData to map to the plot range.
How can I do this in Mathematica?
BTW, to insert colorbars using Mathematica, you can look at this function
Here's a function applied to a surface:
Plot3D[x + y, {x, -2, 2}, {y, -2, 2},
ColorFunction -> (ColorData["Rainbow", #3] &), Mesh -> {{1}, {1}}]
To look at the top-right corner, with the same color function and scaling, I set ColorFunctionScaling -> False, and manually scale the color function to map the (global) minimum to zero and the maximum to one using Rescale:
Plot3D[x + y, {x, 1, 2}, {y, 1, 2}, ColorFunctionScaling -> False,
ColorFunction -> (ColorData["Rainbow", Rescale[#3, {-4, 4}, {0, 1}]] &)]

How to plot |z-1| = 2 in a specified domain in Mathematica?

How do I plot |z-1| = 2, from -10 to +10 in the real line and from -10i to +10i on the complex line? I've been trying for ages and seems like I can't get it right. Z stands for a complex number!
Also, could I use as well the x+iy notation in mathematica? or a+ib?
Thanks
For a contour plot:
ContourPlot[Abs[x + I y] == 2, {x, -10, 10}, {y, -10, 10}]
To just plot a (real-valued) function:
Plot3D[Abs[x + I y], {x, -10, 10}, {y, -10, 10}]
Choice of variable names is, of course, completely arbitrary.
Just for fun: with some smart choices, you can plot a complex-valued complex function, for example by piecing together Plot3D or ContourPlot3D with Animate.

Resources