How to declare variable in Mathematica and use them later - wolfram-mathematica

I am very new to Mathematica and I was curious about how one can declare variable and use them later in function. I tried to do as below but I'm getting empty result in Graph
Here I've declared two variable one and two and tried to use them later but it didn't output the result.
Thanks in advance.

Try this
one:=3*x^2/2;
two:=x;
Show[Plot[one/two,{x,0,15}],ListPlot[{1,3,7,9,10,11,14}]]

Related

How to rewrite a variable in a for cycle for Ti-Basic

I'm trying to program a numerical method in my ti-89, in TI-Basic language, the problem is that when I overwrite the variable inside the loop it doesn't do it, I'm new to this language and I don't know if I'm omitting some detail behind
Item()
prgm
Input "f(x)",a //call the function in text mode
define s(z,t) = a //convert the text into a function
local xa,ya //declare local variables
x->xa //assign values that I already have saved to local variables
y->ya
local i
For i,1,10
s(xa,ya)->xa //evaluate the function, and I have to rewrite the first parameter
EndFor
EndPrgm
I'm not entirely sure what's causing the problem, but I would try using a list to store the data instead if that is all there is. You can rewrite the list and clear it easily. You can store strings in these lists as well. Lists can be edited at all times, which will fix your problem. Although this solution might not be as efficient, it should work. I don't have my calculator with me so I can't test it, sorry :(. These two functions below add things to the list and clear the list.
L1(x,y,z)
ClrList L1
Good luck! TI-Basic can be difficult sometimes

Is it possible to have 2 variables point to the same address in memory

Is it possible in Visual Foxpro to have 2 variables that point to the same address in memory. Such that if the value of one of the variables is changed then the other is also changed. I understand that when passing arguments to functions they can be passed by value or reference but I want to know if this is possible in straight code. I think in other languages such as C this is called a pointer but I don't believe VFP has pointers. So if one writes the following code it will output the number 4.
a=4
b=a
a=6
? b && answer 4
But could one write code such as the following where the answer could be 6?
a=4
b=*a && note the inclusion of the asterisk (pointer?) here which won't compile in VFP
a=6
? b
No. There are no pointers or references in Foxpro; as you note, the closest thing to it is passing parameters by reference to functions. You might be able to try to kludge something together (as Jerry mentions) with objects using Access/Assign methods, but even then, all that gets passed to the Assign method is the value being assigned - nothing about whether it was originally another variable, a literal value, an object's property, etc.
You could simulate it by using an array or a table. The variables would contain only the array index or record number (or other index) as a reference, and you'd have to get the actual value from the array or table.
Take a look at the Visual Foxpro Access and Assign Methods. These methods can be used to execute code when querying a property or trying to change the value of a property. Below is a link that shows an example:
Access and Assign Example
You could do something like this:
a=4
b='a'
a=6
?&b

Mathematica - can I define a block of code using a single variable?

It has been a while since I've used Mathematica, and I looked all throughout the help menu. I think one problem I'm having is that I do not know what exactly to look up. I have a block of code, with things like appending lists and doing basic math, that I want to define as a single variable.
My goal is to loop through a sequence and when needed I wanted to call a block of code that I will be using several times throughout the loop. I am guessing I should just put it all in a loop anyway, but I would like to be able to define it all as one function.
It seems like this should be an easy and straightforward procedure. Am I missing something simple?
This is the basic format for a function definition in Mathematica.
myFunc[par1_,par2_]:=Module[{localVar1,localVar2},
statement1; statement2; returnStatement ]
Your question is not entirely clear, but I interpret that you want something like this:
facRand[] :=
({b, x} = Last#FactorInteger[RandomInteger[1*^12]]; Print[b])
Now every time facRand[] is called a new random integer is factored, global variables b and x are assigned, and the value of b is printed. This could also be done with Function:
Clear[facRand]
facRand =
({b, x} = Last#FactorInteger[RandomInteger[1*^12]]; Print[b]) &
This is also called with facRand[]. This form is standard, and allows addressing or passing the symbol facRand without triggering evaluation.

Correct use of findall/3, especially the first template argument

i know there is a build-in function findall/3 in prolog,
and im trying to find the total numbers of hours(Thrs) and store them in a list, then sum the list up. but it doesnt work for me. here is my code:
totalLecHrs(LN,THrs) :-
lecturer(LN,LId),
findall(Thrs, lectureSegmentHrs(CC,LId,B,E,THrs),L),
sumList(L,Thrs).
could you tell me what's wrong with it? thanks a lot.
You need to use a "dummy" variable for Hours in the findall/3 subgoal. What you wrote uses THrs both as the return value for sumList/2 and as the variable to be listed in L by findall/3. Use X as the first argument of findall and in the corresponding subgoal lectureSegmentHrs/5 as the last argument.
It looks like the problem is that you're using the same variable (Thrs) twice for different things. However it's hard to tell as you've also used different capitalisation in different places. Change the findall line so that the initial variable has the same capitalisation in the lectureSegmentHrs call. Then use a different variable completely to get the final output value (ie the one that appears in sumList and in the return slot of the entire predicate).
You need to use a different variable because Prolog does not support variable reassignment. In a logical language, the notion of reassigning a variable is inherently impossible. Something like the following may seem sensible...
...
X = 10,
X = 11,
...
But you have to remember that , in Prolog is the conjunction operator. You're effectively telling Prolog to find a solution to your problem where X is both 10 and 11 at the same time. So it's obviously going to tell you that that can't be done.
Instead you have to just make up new variable names as you go along. Sometimes this does get a bit annoying but it's just goes with the territory of a logical languages.

How can I get a list of all variables in a binding?

Does a Binding know the variables it stores? Or do I always have to use this in conjunction with local_variables?
You get the list of local variables in a binding by doing eval("local_variables", the_binding).
I'm not sure whether that's what you meant by "Or do I always have to use this in conjunction with local_variables?", but there's no way to get the list without invoking local_variables somehow.
This is an older question, so the answer is correct for Ruby version <= 2
But since 2.1 new methods have appeared to get/set and list local variables:
local_variable_get / local_variable_set / local_variable_defined?

Resources