Having come from Matlab I am struggling to work out why the following does not work:
plot(x=rand(10),y=rand(10))
Produces a graph correctly.
x=rand(10)
y=rand(10)
plot(x,y)
produces error:
ERROR: plot has no method matching plot(::Array(Float64,1),::Array(Float64,1))
I would be very grateful if someone coould explain to me why embeding the code within the plot line produces a result, but defining the variables beforehand results in an error. Logic says they should produce the same result.
I am using Julia v 0.3.1 and have loaded Gadfly as charting tool.
In the first case, you are using keyword argument syntax, not assigning to variables x and y (the meaning of = inside function calls is special). To get the same effect in the second case, you should use
x=rand(10)
y=rand(10)
plot(x=x,y=y)
which passes the value in the variable x in the keyword argument x to plot, and the value in the variable y in the keyword argument y.
In case you didn't. Write this before your code:
using plots
plyplot()
Related
I’m required to use/learn R for a new lecture at uni and I’m currently struggling a bit with its syntax. I want to plot (via curve) a simple function, but I can’t seem to get it working with an inline lambda-like function.
I’ve tried the following:
> curve( function(x) x^2 )
Error in curve(function(x) x^2) :
'expr' did not evaluate to an object of length 'n'
When I however store the function in a variable first, it works:
> quad <- function(x) x^2
> curve( quad )
Is such an inline use not allowed in R? Is there any other way to make this work without defining an extra function? Thanks!
Just for completeness. You can use "lambda-like" (anonymous) functions in R but if you want to put them to immediate use, you need to enclose the function definition in parentheses or curly braces:
(function (x) x+1) (1)
{function (x,y) x^y} (2,3)
In the case of curve the first argument is either expression or a function name - but if it is a function name then it is first converted to an expression. (See first few lines in the source code of curve). So if its' not a function name, you'll need an expression – which may contain a "lambda" function:
curve((function (x) x^2)(x))
If you want to use a function (as opposed to its name) as the argument, you can use plot.function:
plot(function(x) x^2)
From R 4.1 on, you can use \(x) lambda-like shorthand:
R now provides a shorthand notation for creating anonymous functions,
e.g. \(x) x + 1 is parsed as function(x) x + 1.
With function(x) x^2:
(\(x) x^2)(2)
#[1] 4
This can be used with curve :
curve((\(x) x^2)(x))
But as stated in comments, in this case an expression is more straightforward :
curve(x^2)
You have to look at the source of curve to appreciate what is happening (just type curve at the prompt and press enter).
There you can find how the expression passed is parsed.
The only way a function is discovered as being just that, is when only its name is passed along (see the is.namepart). If that is not the case, the expression is called for every x. In your case: for every x, the result is a function, which is not a happy thought for plotting...
So in short: no you cannot do what you tried, but as #ROLO indicated, you can immediately pass the function body, which will be parsed as an expression (and should contain x). If this holds multiple statements, just enclose them in curly braces.
I am new to R code and trying different functions in R. However when I tried to use the sqrt function, it always give me a error: argument "which" is missing. When I tried to see what sqrt really is, this was showing in my console. y = if(which==1){sqrt(x)} else {log(x)}. Does anyone have idea why my sqrt function is different than the other's?
I am trying to plot a function in Julia, but keep getting errors. I don't understand what is wrong. The input and output of $\varphi$ is a scalar. I've used x=1530:1545 and still get an error-- can anyone enlighten me? I am very confused.
I am using Julia 0.7.
EDIT:
I got it to work with a slight modification--I changed
x = 1530:1545
added the following two lines
y = t.(x)
plot(x,y)
Why did I have to do this though?
This feature is currently not available in PyPlots.jl, if you would like to have it in the future, your best bet is to file an issue.
However, you can get that functionality via Plots.jl and using PyPlot as a backend.
It would look like this (I'll take a simpler function):
using Plots
pyplot()
start_point = 0
end_point = 10
plot_range = start_point:end_point
plot(sqrt,plot_range) # if you want the function exactly at 0,1,2,3...
plot(plot_range,sqrt) # works the same
plot(sqrt,start_point,end_point) # automatically chooses the interior points
With Maxima, I want to plot the value of a parameter depending on time by solving an equation for that specific parameter. I am new to Maxima and I already struggle with the beginning of my calculations.
I use the following equation m which I want to solve for L:
m= m_I - (m_I-m_R)/(1+%e^(-s_R*(t-L)))
solve(%,L);
which gives me
L=(t*s_R-log(m_I/(m-m_R)-m/(m-m_R)))/s_R
as output. If I now assign values to all parameters except L and t
ev(%,m=0.5,m_I=1,m_R=0.1,s_R=0.01);
plot2d(%,[t,0,10]);
I get the error message
"plot2d: expression evaluates to non-numeric value everywhere in
plotting range. plot2d: nothing to plot."
I know this is very basic but I still don't know what I am doing wrong. I also tried to use a function m(t):=... instead of an expression m=..., with the same result.
Note that solve has returned a list containing one element, which is an equation. In order to plot the result, you need to isolate the right-hand side of the equation, because that's what plot2d understands (it doesn't know what to do with the output of solve otherwise).
My advice is to get the part of the solve result that you want first, and then plot that. Something like:
solve (...);
my_equation : %[1];
my_equation_rhs : rhs(%);
plot2d (my_equation_rhs, [t, 0, 10]);
It is a deficiency of plot2d that it doesn't know what to do with the result of solve; sorry about that.
When I try:
x=c(1,2,3)
...I get the message unused argument (3)
When I type x=c(1,2) it takes the function but when I type x it gives me a value of [3]. I want to create a simple vector of numbers but I don't know why it is giving me this message.
I presume you created a user-defined function called c, thereby replacing the built-in base-r function. Try rm("c") and then everything should work as expected again.