Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'Program written in a different language' topicStudent Support Forum > General > "Program written in a different language"

< Previous CommentHelp | Reply To Comment | Reply To Topic
Author Comment/Response
Bill Simpson
07/14/13 11:06am

I tried to maintain the style of the original program, but the two languages do many things very differently.

Carefully "scrape and paste" this into a notebook.

There are almost certainly errors remaining in this and some of the translation makes this prone to more errors if someone begins making changes.

You should run the notebook and the original program side by side with a variety of inputs to try to expose any errors and then see if you can figure out how to fix them.

settitle[s_] := (title = s);
sethlabel[s_] := (hlabel = s);
setvlabel[s_] := (vlabel = s);

datagraph[r_, field_, pstyle_, lstyle_, color_] := (
graphiclist = {ListPlot[Transpose[{r, field}],
AxesLabel -> {hlabel, vlabel}, PlotLabel -> title,
PlotMarkers -> If[pstyle == 3, "*", "\[CenterDot]"],
Joined -> Equal[lstyle, 1], PlotStyle -> color]};
Print[Show[graphiclist]]
);

adddatagraph[r_, field_, pstyle_, lstyle_, color_] := (
graphiclist = Append[graphiclist, ListPlot[Transpose[{r, field}],
PlotMarkers -> If[pstyle == 4, "\[EmptyCircle]", "\[CenterDot]"],
Joined -> Equal[lstyle, 1], PlotStyle -> color]];
Print[Show[graphiclist]]
);

(*!initialize variables*)
initialize [] := (
max = Input["number of grid steps -> "];
r = field = Table[0, {max}]
);

calculate[max_, length_] := (
For[i = 1, i <= max, i++,
dz = 0.1;
field[[i]] = 0;
For[z = -length, z <= length, z += dz,
cr = Sqrt[(i/max)^2 + z^2];
field[[i]] = field[[i]] + dz*(i/max)/cr^3
];
r[[i]] = i/max
]
);

display[r_, field_, max_] := (
settitle["Magnetic field from a straight wire"];
sethlabel["R"];
setvlabel["B"];
datagraph[r, field, 3, 0, Black]
);

exact[] := (
max = 100;
r = Table[i/max, {i, max}];
field = Table[1/(i/max), {i, max}];
adddatagraph[r, field, 1, 1, Black]
);

(*magnetic field from a straight wire*)
lstart = .5;
lfinish = 10;
initialize[];
length = lstart;
i = 1;
While[True,
calculate[max, length];
If[length == lstart, display[r, field, max]];
If[length > lstart, adddatagraph[r, field, 4, 0, Black]];
i = i + 1;
length = 3*length;
If[length > lfinish, Break[]]
];
exact[r, field];

URL: ,

Subject (listing for 'Program written in a different language')
Author Date Posted
Program written in a different language Sean 07/13/13 3:52pm
Re: Program written in a different language Bill Simpson 07/14/13 11:06am
< Previous CommentHelp | Reply To Comment | Reply To Topic