RE: General--Difficulties in Understanding Mathematica Syntax

• To: mathgroup at smc.vnet.net
• Subject: [mg68963] RE: [mg68908] General--Difficulties in Understanding Mathematica Syntax
• From: "David Park" <djmp at earthlink.net>
• Date: Fri, 25 Aug 2006 05:35:24 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```You will probably get a number of different answers on this with various
approaches and some of them may be better than mine. But here is my
approach.

First I would recommend working through as much of Part I in The Mathematica
Book as seems relevant to you in order understand the basic syntax and usage
of Mathematica, and become familiar with the more common commands.

Secondly, don't think of Mathematica as either a 'super calculator' or as a
'programming language', althought it is in part these things. Think of it as
a piece of paper on which you are going to write down and do your
mathematics. Keep the mathematics in the foreground and let Mathematica be
in the background. You want to write down your specifications and
definitions and then perform mathematical operations on them. You have
actually gotten a fairly good start on doing that.

I would define your data values as a set of rules as follows:

data = {p -> 10.3, l -> 0.45, t -> 0.9, r0 -> 0.025, d -> 0.1643};

I started all the symbol names with small case letters. This is not
absolutely necessary but Mathematica always starts the reserved names with a
capital letter so this avoids any possible conflict. By using rules and
substituting when we want to we are still free to use these symbols as
strictly symbolic quantities. We might want to do this in equations and to
manipulate the equations before we actually substitute values. In your
problem we don't actually do this but it is still good practice not to Set
values to commonly used symbols. It is a common source of error. Also, you
had units 'mm' I think on some of the quantities. If we are going to do
numerical operations we want to get rid of the units. (Keeping the implied
units straight is another topic.)

Next, I rewrite the definitions you gave.

r[xk_, yk_] [x_, y_] := Sqrt[(x - xk)^2 + (y - yk)^2]

Intensity[xk_, yk_][x_, y_] := (p/(2*Pi*r[xk, yk][x, y]*l))*
t^(r[xk, yk][x, y] - r0);

The main thing I did here was put xk,yk in as subvalues and x,y as regular
arguments. I imagine that these are all used as variables in your final
output explorations. We might also consider adding p,l,t,r0 as parameters
but I would imagine that they are relatively fixed.

We could then calculate your integrated intensity at a specific location.
For example. I substitute the data values and use NIntegrate instead of
Integrate. (When I tried to integrate symbolically it took too long.)

NIntegrate[Intensity[xk, yk][0, 0] /. data, {xk, 0.1, -0.1}, {yk,
0.1, -0.1}]
2.56003

Suppose we want to see what this integrated intensity looks like along the
x-axis. We can write the following definition.

integral[s_] :=
NIntegrate[
Intensity[xk, yk][s, 0] /. data, {xk, 0.1, -0.1}, {yk, 0.1, -0.1}]

Then we could generate a set of values along the x axis.

xvals = Table[{s, integral[s]}, {s, 0, 2, 0.1}]
{{0, 2.56003}, {0.1, 1.74232}, {0.2, 0.743068}, {0.3, 0.480509}, {0.4,
0.353898}, {0.5, 0.279127}, {0.6, 0.229701}, {0.7, 0.194581}, {0.8,
0.168337}, {0.9, 0.147981}, {1., 0.131733}, {1.1, 0.118467}, {1.2,
0.107431}, {1.3, 0.0981102}, {1.4, 0.0901345}, {1.5, 0.0832341}, {1.6,
0.0772066}, {1.7, 0.0718977}, {1.8, 0.0671871}, {1.9, 0.0629802}, {2.,
0.0592012}}

There was also a warning message but I don't know if we have to worry much

We could then define this as a function by interpolating on the values
calculated above.

f[x_] = Interpolation[xvals][x]
InterpolatingFunction[{{0., 2.}}, <>][x]

And then we could plot the resulting function.

Plot[f[x], {x, 0, 2},
Frame -> True,
FrameLabel -> {x, f},
PlotRange -> All,
PlotLabel -> "Integral of Intensity Along x Axis",
ImageSize -> 500];

There may be better approaches to this particular problem but I hope that I
have conveyed the idea that we are just doing the mathematics and writing it
down in the notebook and it is really not at all like formal 'computer
science programming'.

David Park

From: madhukarrapaka at yahoo.com [mailto:madhukarrapaka at yahoo.com]
To: mathgroup at smc.vnet.net

Hi Mathematica Experts,

Sorry for a lengthy query.

I am new to Mathematica. The syntax of mathematica is quite different from
other programming languages.
I am confused how to use loops like "If",FOR;DO;...

Here i am giving details of a problem i want to solve. I made an attempt to
solve this. Please go thru it and give me necessary corrections.

Here i am giving the details:
P = 10.3;l = 0.45mm;T = 0.9;r0 = 0.025mm;d = 0.1643mm;
r[xk_, yk_] := (Sqrt[(x - xk)^2 + (y - yk)^2]);
Intensity[xk_, yk_] := (P/(2*Pi*r[xk, yk]*l))*T^(r[xk, yk] - r0);
Imoy=[Integrate[Intensity[xk, yk]],{xk, 0.1, -0.1}, {yk, 0.1, -0.1}] ;
Print[Imoy]

Then i am getting some value, which is not the correct one.
How to check the output for each step in Mathematica(for example: if i want
to find out the values of "r", In C language i can use Printf statement to
print the values of "r" for each xk,yk)

And if i want to evaluate the "Intensity[xk_, yk_] " depending on a
condition (say: r[xk,yk]<d) [I think it can be done by using IF statement,
but i have no clue how to use the IF statement in MATHEMATICA].

It would be better if one can provide me some examples., or documentation
which contains some solved examples in Mathematica.

Link to the forum page for this post:
http://www.mathematica-users.org/webMathematica/wiki/wiki.jsp?pageName=Speci
al:Forum_ViewTopic&pid=12943#p12943
Posted through http://www.mathematica-users.org [[postId=12943]]

```

• Prev by Date: nullspaces
• Next by Date: Re: benchmark...why don't you send it back?
• Previous by thread: Re: General--Difficulties in Understanding Mathematica Syntax
• Next by thread: Re: General--Difficulties in Understanding Mathematica Syntax