I'm trying to run a cobweb code in Mathematica and I need the following script:
ClearAll[CobwebPlot]
Options[CobwebPlot]=Join[{CobStyle->Automatic},Options[Graphics]];
CobwebPlot[f_,start_?NumericQ,n_,xrange:{xmin_,xmax_},opts:OptionsPattern[]]:=Module[{cob,x,g1,coor},
cob=NestList[f,N[start],n];
coor = Partition[Riffle[cob,cob],2,1];
coor[[1,2]]=0;
cobstyle=OptionValue[CobwebPlot,CobStyle];
cobstyle=If[cobstyle===Automatic,Red,cobstyle];
g1=Graphics[{cobstyle,Line[coor]}];
Show[{Plot[{x,f[x]},{x,xmin,xmax},PlotStyle->{{Thick,Black},Black}],g1},FilterRules[{opts},Options[Graphics]]]
]
Manipulate[CobwebPlot[Sqrt[3#-1]&,\[Alpha],40,{0,5},PlotRange->{{0,4.5},{0,3.65}},Frame->True,Axes->False,CobStyle->Directive[Dashed,Red],PlotRangePadding->None],{\[Alpha],0.5,4.375}]
I found the script online but I don't understand some features, such as what is the purpose of the following characters, # and &, in the Manipulate[] segment of the code:
Manipulate[CobwebPlot[Sqrt[3#-1]&,\[Alpha],40,{0,5},PlotRange->{{0,4.5},{0,3.65}},Frame->True,Axes->False,CobStyle->Directive[Dashed,Red],PlotRangePadding->None],{\[Alpha],0.5,4.375}]
Can you help me?
See this Mathematica documentation page on pure functions, or what other languages call anonymous functions, or lambda functions.
To give a cute example, suppose you have the function
doItTwice[x_,f_] := f[f[x]];
Now say you want to use this function to square the number seven twice. One way to do this would be to define a square function like this:
square[x_] := x^2;
doItTwice[7, square]
Well, there is a cleaner way to do this by simply writing the square function as a pure function, which would look like (#^2)&. The # is the parameter to the pure function, and the & is just there to indicate that it's a pure function. Really the parenthesis aren't even necessary, so you could write #^2&. Anyways, the following code is now a cleaner way to square seven twice:
doItTwice[7, (#^2)&]
Attempting to write more queries in F# through LinqPad I ran into an interesting thing with the where clause.
let dc = new TypedDataContext()
let q = query {
for o in dc.OrderItem do
where (o.Description.Contains("spam"))
select o
}
q |> Dump
If I remove the parenthesis around o.Description.Contains("spam") I get the often seen error message below.
Successive arguments should be separated by spaces or tupled, and arguments involving function or method applications should be parenthesized (using external F# compiler)
When I see this error I normally realize that I'm not giving enough information in order for the compiler to understand that I'm attempting to chain off of the result of a prior method call or property get accessor invocation but in this case it is unclear to me. Additionally I'd be curious if there is a more idiomatic way of satisfying the conditions of the compiler without having to reach around the expression to add both open and closing parenthesis.
You'd see the same result if you did something like this:
let f x = 1
f o.Description.Contains("spam")
As the message implies, if you're using the result of a method call (Contains in this case) as an argument to a function, then the method call needs to be parenthesized. Even though where is a query operator rather than a true function, the same result applies.
If I have a function:
Foo[x_] := If[x==2, Print#"Two", Print#"No"]
Then if I write the following:
Foo[oops]; Foo[5]
Where oops is a misspelled name for a global variable, the result is that the call to Foo[oops] just falls through rather than giving an error. I know why this is - because it creates a symbolic expression that, since it is not evaluated, does not do anything - but it's very awkward for procedural programming. Is there any way to specify that a function or expression must be completely evaluated and to give an error or return an appropriate value if it isn't?
If you want to require a numeric argument do something like this:
foo[x_?NumericQ] := whatever
foo[x_] := Print["Error"]
Be sure to Clear your original definition before defining this way.
In your example you could alternately work with the three argument form of If
I have to run ToExpression["Test#test"] and I want to return test#test, but the
function always return Test[test].
I tried to Unprotect, Clear, ClearAll, Remove ["#"] or [#], but it doesn't work.
Any ideas?
Test#test and Test[test] are two different notations for the very same Mathematica expression. If you convert the string "Test#test" to a Mathematica expression, any information about how it was entered is lost---only the expression structure is retained.
You should tell us why you want to "return test#test", as you said. It seems to me you have some serious confusion about how Mathematica works. Just explain what you want to achieve.
Assuming I have this code:
function question($argument)
{
var $q = "What does ($argument) mean?";
}
Can any tell me is there any other word (or phrase) that defines what an argument is?
I'm asking this because English is my second language, and I can't for life find a word in my language that defines "argument" in "programming".
I understand how arguments work, and what they are for, I just need a synonym word or phrase to be able to translate it to my language to make it easy to use and understand.
The best thing that I came up with (in my language) is (Passed Variable(s)), does that sound right? Is there any better wording?
Thanks
Parameters
Does that help?
("Passed Variables" is close ... and might work fine in your language)
I wouldn't use "passed variable" because arguments do not have to be variables.
Perhaps the most common usage of the term is seen in this example. Consider
// A function definition
function f(x, y) {
....
}
// A function call
f(57/p*q+4, z);
Most people would call x and y parameters, and call 57/p*q+4 and z arguments. Note that parameters are variables (unless the language has pattern-matching, not too common) and that arguments can be arbitrary expressions.
Now you may hear people call x and y "formal parameters" while the arguments are "actual parameters" but IMHO this distinction is a little old-fashioned. I may be wrong though.
The thing is that the argument is the expression that is passed to the parameter in a function call. So maybe "passed expression" is better than "passed variable" at the very least. Have fun translating. One fun thing about the vocabulary of computing is that almost every word (function, procedure, type, label, constant, variable, expression, declaration, statement, operator, argument, parameter, etc.) is just borrowed from a plain old English word. There aren't too many novel terms.
On the calling side it's an argument, on the function side it's a parameter.
"Parameter" vs "Argument"
An argument is what you pass into a function (also known as a subroutine). Arguments are also known as parameters. A function might take an argument and use it to calculate something or modify the argument itself.
Arguments are the variables of the functions that works during calling them.
And
parameters are also the variables of the functions that works during returning value to the program by that function.