I have to plot the function f(x) = ln(20 - e^x) in Octave, and I use the command:
x = -5:0.1:5;
y = log(20 - exp(x));
plot(x,y)
But the graph is not correct, because when I check in Wolfram Alpha it is not the same. Any help is appreciated!
You plotted in Octave ln(20-e^x):
whereas what you put into Wolfram Alpha was e^x + e^y = 20, which looks like this:
Which is the exact same. The only difference here is that for e^x+e^y=20 Wolfram Alpha plots only the real solution (the blue line), whereas for ln(20-e^x) both Wolfram Alpha and Octave plot the full set of solution, so including imaginary solutions (although Octave plots only the real part of the complex solution).
If you look carefully you see that for x<ln(20)the imaginary part shown in Wolfram Alpha is 0, whereas for x>ln(20) there's an imaginary part (incidentally of y=ln(20)). Octave just plots only the real parts, as it ignores imaginary parts when plotting a complex signal. Just check whos y on your command line and it'll tell you it is a complex variable.
I'm on MATLAB, but your console output should be the similar:
>> x = -5:0.1:5;
y = log(20 - exp(x));
plot(x,y)
Warning: Imaginary parts of complex X and/or Y arguments ignored
>> whos y
Name Size Bytes Class Attributes
y 1x101 1616 double complex
which tells you A) when you plot the function that it is a complex signal and B) that y is indeed complex, as it should be for values of x>ln(20).
Related
This is more of a general Maths question (might be silly even). But in high school we learn to identify the roots of an equation via it's plot right.
For example, for the equation
y = x^2 - 1
The blue line would show us the roots. This is when the blue line crosses x, so +- 1.
Now, if we said that the equation had a real and an imaginary part, so that it is
y = x^2 - 1 + (x^2 - 0.5)i
as given in the Mathematica screenshot, then we have a real part which crosses zero, and an imaginary part which also crosses zero but at a different x. So my question is: is it possible to identify the roots of such an equation by simply looking at the real and imaginary parts of the plot?
Note: part of my confusion is that if I use FindRoot, in Mathematica, I get either 0.877659 - 0.142424i or -0.877659 + 0.142424i. So might be some fundamental property in Maths I don't know about which prevents one from identifying roots of a complex function through separating real and imaginary parts...
we have a real part which crosses zero, and an imaginary part which also crosses zero but at a different x.
Those are graphs of the real and imaginary parts plotted for real values of x. If they both crossed the horizontal axis at the same point(s), that would mean the equation has real root(s), since both real and imaginary parts would be zero for some real value of x. However, this equation has no real roots, so the crossing points are different.
So my question is: is it possible to identify the roots of such an equation by simply looking at the real and imaginary parts of the plot?
f(x) = x^2 - 1 + i (x^2 - 0.5) is a complex function of a complex variable, which maps a complex variable x = a + i b to the complex value f(x) = Re(f(x)) + i Im(f(x)).
Each of Re(f(x)) and Im(f(x)) is a real function of a complex variable. Such functions can be plotted in 3D by representing x = a + i b as a point in the (a, b) plane, and the value of the function along the third dimension, say c. For example, f(x) has the following graphs for the real and imaginary parts.
The cross-sections of the two surfaces by the horizontal plane c = 0 are pairs of curves where each function is zero, respectively. It follows that the intersections of those curves are the points where Re(f(x)) = Im(f(x)) = 0, which means they are the roots of the equation f(x) = 0.
Since f(x) = 0 is a quadratic equation, it must have two roots, and those two points are in fact ±(0.877659 - 0.142424 i), as can be verified by direct calculation.
The diagram and the graph
I have made the above diagram and I placed the values of 1,1 as the parameters of INTEGRAL_f and GAINBLK_f respectively,and the other parameters are as default.
I have no idea why this graph is plotted since I thought that there should be no factor(s) which compose the exponential function.
Can anyone tell me what I am missing?
That's just math. You're have the system (here noted in the Laplace domain) y = 1/s*u and apply to it the positive feedback u = u+y, hence you end up (after solving for y) with the system y = 1/(s-1)*u, which is in state space, with your initial condition of integral block
x' = x+u,
x(0) = 1,
since you have no input, you obtain
x' = x,
x(0) = 1,
which yields x(t)=exp(t). If you want to see something interesting, just take 0 as initial condition of the integral block and add an input. However, if you keep this positive feedback your system is unstable...
This should hopefully not be a very hard question—I'm just not very experienced in R.
If I want to graph a simple sine wave, all I have to is:
x=seq(-20,20,0.001)
y=seq(-20,20,0.001)
y=sin(x)
plot(x,y,type="l")
But let's say I want to graph a relationship with trigonometric functions on both sides, such as sin(x) = cos(y). Typing:
sin(x) = cos(y)
Gives me the following error
Error in sin(x) = cos(y) : could not find function "sin<-"
Now, the obvious solution is to just rearrange it in terms of one variable, such as x = asin(cos(y)). But with much more complicated equations with multiple nested trigonometric functions on both sides, this no longer becomes viable.
I'm sure I'm missing something extremely obvious, but what is it?
If you want to plot the relationship sin on the x axis and cos on the y axis:
plot(sin(x), cos(y), type = "l")
Or did I misunderstand the question?
The error is caused by your equal sign. The expression sin(x) = cos(y) is an assignment. If you would like to check where they are equal, then you should write sin(x) == cos(x).
I want to optimize a function for a multivariate surface given a range of values for one variable.
For example, take the equation for the following quadratic surface:
z = x + x^2 + xy + y^2 + y.
How would I find values of y that maximize z given all possible values of x? The result should be a line along the surface that maximizes z at every value of x.
I have found a lot of resources online that explain how to find maxima and minima, as well as saddle points, but I am not sure if that approach will be relevant - the slope of the surface along that line will usually not be 0, so I don't think it makes sense to use derivatives here.
I am new to calculus and mathematical optimization. I would be thrilled if someone would point me to a resource that could help me out with this problem
Thank you!
Background
I read here that newton method fails on function x^(1/3) when it's inital step is 1. I am tring to test it in julia jupyter notebook.
I want to print a plot of function x^(1/3)
then I want to run code
f = x->x^(1/3)
D(f) = x->ForwardDiff.derivative(f, float(x))
x = find_zero((f, D(f)),1, Roots.Newton(),verbose=true)
Problem:
How to print chart of function x^(1/3) in range eg.(-1,1)
I tried
f = x->x^(1/3)
plot(f,-1,1)
I got
I changed code to
f = x->(x+0im)^(1/3)
plot(f,-1,1)
I got
I want my plot to look like a plot of x^(1/3) in google
However I can not print more than a half of it
That's because x^(1/3) does not always return a real (as in numbers) result or the real cube root of x. For negative numbers, the exponentiation function with some powers like (1/3 or 1.254 and I suppose all non-integers) will return a Complex. For type-stability requirements in Julia, this operation applied to a negative Real gives a DomainError. This behavior is also noted in Frequently Asked Questions section of Julia manual.
julia> (-1)^(1/3)
ERROR: DomainError with -1.0:
Exponentiation yielding a complex result requires a complex argument.
Replace x^y with (x+0im)^y, Complex(x)^y, or similar.
julia> Complex(-1)^(1/3)
0.5 + 0.8660254037844386im
Note that The behavior of returning a complex number for exponentiation of negative values is not really different than, say, MATLAB's behavior
>>> (-1)^(1/3)
ans =
0.5000 + 0.8660i
What you want, however, is to plot the real cube root.
You can go with
plot(x -> x < 0 ? -(-x)^(1//3) : x^(1//3), -1, 1)
to enforce real cube root or use the built-in cbrt function for that instead.
plot(cbrt, -1, 1)
It also has an alias ∛.
plot(∛, -1, 1)
F(x) is an odd function, you just use [0 1] as input variable.
The plot on [-1 0] is deducted as follow
The code is below
import numpy as np
import matplotlib.pyplot as plt
# Function f
f = lambda x: x**(1/3)
fig, ax = plt.subplots()
x1 = np.linspace(0, 1, num = 100)
x2 = np.linspace(-1, 0, num = 100)
ax.plot(x1, f(x1))
ax.plot(x2, -f(x1[::-1]))
ax.axhline(y=0, color='k')
ax.axvline(x=0, color='k')
plt.show()
Plot
That Google plot makes no sense to me. For x > 0 it's ok, but for negative values of x the correct result is complex, and the Google plot appears to be showing the negative of the absolute value, which is strange.
Below you can see the output from Matlab, which is less fussy about types than Julia. As you can see it does not agree with your plot.
From the plot you can see that positive x values give a real-valued answer, while negative x give a complex-valued answer. The reason Julia errors for negative inputs, is that they are very concerned with type stability. Having the output type of a function depend on the input value would cause a type instability, which harms performance. This is less of a concern for Matlab or Python, etc.
If you want a plot similar the above in Julia, you can define your function like this:
f = x -> sign(x) * abs(complex(x)^(1/3))
Edit: Actually, a better and faster version is
f = x -> sign(x) * abs(x)^(1/3)
Yeah, it looks awkward, but that's because you want a really strange plot, which imho makes no sense for the function x^(1/3).