Defining a procedure that plots a function with its derivative but yields no output - sage

I'm trying to define a procedure that plots a function on an interval (a,b) along with its first derivative
def graph(f,a,b):
g(x)=f
h(x)=g.diff(x,1)
g1=plot(g,a,b)
g2=plot(h,a,b)
(g1+g2).show
but when i try it, for example with
graph(x^2,1,2)
there is no output.
I'd appreciate any hints/help

so meanwhile I've figured it out
def graph(f,a,b):
g(x)=f
h(x)=g.diff(x,1)
g1=plot(g,a,b)
g2=plot(h,a,b)
p=(g1+g2)
return p
does the trick

Related

problem with drawing graph of sum function in r

Let's take sample 'y' of length 100 from N(0,1) distribution and function g(x)=x^2+7.
I want to make a graph of function $h(x)=\sum_{i=1}^{100}g(x-y[i])$
So
y=rnorm(100,0,1)
g=function(x){x^2+7}
h=function(x){sum(g(x-y))}
And now expressions like
curve(h)
plot(h)
lines(h)
doesnt work, can You please help me how can i draw graph of function h?

SCILAB undefined vairable error when using 'polarplot'

I am getting the following error while running polarplot in scilab
// Program to plot using polarplot function
t= 0:.01:2*%pi;
polarplot(sin(t))
xtitle('Using polarplot'
result:
exec('D:\mangesh\SCILAB PROJ\sample\polarplot.sce', -1)
at line 13 of function polarplot ( C:\PROGRA~1\SCILAB~1.1\modules\graphics\macros\polarplot.sci line 25 )
at line 3 of executed file D:\mangesh\SCILAB PROJ\sample\polarplot.sce
Undefined variable: rho
The polarplot function requires at least 2 input arguments theta and rho,
In your example you forgot to give the evolution of the radius. for example:
polarplot(sin(t), ones(t))
As indicated by the other user as well the polarplot function requires at least two input vectors, like most of other plotting functions. It this case you probably want something like:
// Program to plot using polarplot function
t = 0:.01:2*%pi;
polarplot(t, sin(t));
xtitle('Using polarplot');
which yields:

How plot heart curve in scilab?

I have been looking in the forum and within the graphical functions of the program help, how to graph the following function:
x2=[-2:0.02:2]';x1=[-1:0.01:1]';
function val = Heart(x1, x2)
val=(1.2*x2-sqrt(abs(x1)))^2+x1^2-1;
endfunction
And I have not found anything to guide me.
I try to reproduce this maple plot:
Heart curve
an other solution is to solve the equation (1.2*x2-sqrt(abs(x1)))^2+x1^2-1 = c
for x2
The solution is quite easy
and one can found 2 branches
x2=(5/6)(sqrt(abs(x1))+sqrt(c+1-x1^2));
and
x2=(5/6)(sqrt(abs(x1))-sqrt(c+1-x1^2));
c=0;
x1=linspace(-1,1,200);
x1r=x1($:-1:1);
x2=(5/6)*[ (sqrt(abs(x1))+sqrt(c+1-x1^2)) (sqrt(abs(x1r))-sqrt(c+1- x1r^2))];
clf;plot([x1 x1r],x2);
Since your defining a 3D surface, you can use contour and contour2d: as said by luispauloml, you can pass the function directly as a parameter.
x1=[-1:0.01:1]
x2=[-2:0.02:2]
function val = Heart(x1, x2)
val=(1.2*x2-sqrt(abs(x1))).^2+x1.^2-1; // switched ^ to .^ to handle vectors
endfunction;
figure()
xlabel('x1')
ylabel('x2')
contour2d(x1,x2,Heart,[0 0]);
a=gca()
hline=a.children.children(1)
hline.foreground=color('red')
hline.thickness=2

Is there a way to plot graph in julia while executing loops?

Let us consider the following scenario .
for x in range (1,100)
for y in range (2,500)
#plot(f(x),g(y))
end
end
where f(x) and g(y) are some user defined functions.
The output must be the desired points on plane.
Is there any way in julia to do like what I need ?
In general I can do like this
for x in range (1,100)
for y in range (2,500)
push!(l,f(x))
push!(m,g(y))
end
end
and then plotting from the two lists l,m as x,y axes respectively.
But now I want to plot points while executing loop.
This is mostly supported in Plots... see https://github.com/tbreloff/Plots.jl/issues/30 for a little more information and some example usage.
use the display function:
for x in 1:100
p = plot(f(x),g(y))
display(p)
sleep(1)
end
(inspired by Andreas Peter on the Julia slack #helpdesk channel)

Plotting Leibniz series

How to plot Leibniz series in R for above? Basically I am looking for R commands.
Let's see if I can cobble together an exact transliteration using Reduce which allows cumulative function applications to series. The :-operator is also quite handy for building the underlying series:
plot( pi/4 - Reduce( 'sum' ,
(-1)^(0:200)*(1/(1+2*(0:200))),
acc=TRUE) ) # preserves the intermediate values
This is definitely a homework assignment because I googled the same thing lol. I will help without giving away the answer because you'll learn better if you actually work through the assignment.
At this point, my class did not learn the Reduce function so as an alternative, what you can do is create a function that implements the series: 1 - 1/3 + 1/5 - 1/7 +......, for n iterations (n = 200).
Have the function return a list of values (this would be your y-axis values) and you can plot those for 0:200 (your x-axis values). Then plot a second line graph with y-axis as pi/4 minus the values returned by the function.

Resources