In Sagemath, at some point I end up with the following expression (with b > 0):
Which would be easy to simplify using the
arctan(x) + arctan(1/x) = pi / 2 equality.
My problem is: how do I simplify using that inequality? I tried simplify_trig() and simplify_full() but none of them worked.
Should I use something like assume( arctan(1/sqrt(b-1)) + arctan(sqrt(b-1))) == pi / 2) ? That looks like a ugly hack
Try this:
sage: expr.subs({arctan(1/sqrt(b - 1)): pi/2 - arctan(sqrt(b - 1))})
Related
I got a equation in the following form
f(t) = 2*cos(t) + 3*sin(t)
and I want to reduce it to something like
f(t) = (9 + 4)^(1/2) * cos(t - atan(3/2))
None of the trigonometric simplify functions seems to work. Is there a way to do so?
I have a non-linear optimization problem which, in Mathematica, could be solved as:
FindMaximum[{(81 x + 19)^0.4 + (80 (1 - x) + 20)^0.6, 0 <= x <= 1}, x]
However, now I am on a computer without Mathematica and I would like to solve a similar problem in Python, using the CVXOPT module. I looked at the examples and found linear programs, quadratic programs, and other kinds of programs, but could not find this simple program.
Can I solve such a program with CVXOPT?
I did not find a solution with cvxopt, but I found a much better alternative - cvxpy:
import cvxpy as cp
x = cp.Variable()
prob = cp.Problem(
cp.Maximize((81*x + 19)**0.6 + (80*(1-x)+20)**0.6),
[0 <= x, x <= 1])
prob.solve() # Returns the optimal value.
print("status:", prob.status)
print("optimal value", prob.value)
print("optimal var", x.value)
Prints:
status: optimal
optimal value 23.27298502822502
optimal var 0.5145387371825181
The goal of this assignment is to take the recurrence relation given at the bottom, and then create a recursive function under recFunc(n), as well as a closed function definition underneath nonRecFunc(n). A closed function means our function should solely depend on n, and that its output should
match the recursive function's exactly. Then, find the value for n = 15 and n = 20, and use it as instructed below. You should probably need to use a characteristic equation to solve this problem.
What is the value for nonRecFunc(20) (divided by) nonRecFunc(15), rounded to the nearest integer.
Problem:
Solve the recurrence relation a_n = 12a_n-1 - 32a_n-2 with initial conditions a_0 = 1 and a_1 = 4.
I am confused as to how I should attack this problem and how I can use recursion to solve the issue.
def recFunc(n):
if n == 0:
return 1
elif n == 1:
return 2
else:
return recFunc(n - 1) + 6 * recFunc(n - 2)
def nonRecFunc(n):
return 4/5 * 3 ** n + 1/5 * (-2) ** n
for i in range(0,10):
print(recFunc(i))
print(nonRecFunc(i))
print()
As mentioned in a my comment above, I leave the recursive solution to you.
For the more mathematical question of the non-recursive solution consider this:
you have
x_n = a x_(n-1) + b x_(n-2)
This means that the change of x is more or less proportional to x as x_n and x_(n-1) will be of same order of magnitude. In other words we are looking for a function type giving
df(n)/dn ~ f(n)
This is something exponential. So the above assumption is
x_n = alpha t^n + beta s^n
(later when solving for s and t the motivation for this becomes clear) from the start values we get
alpha + beta = 1
and
alpha t + beta s = 2
The recursion provides
alpha t^n + beta s^n = a ( alpa t^(n-1) + beta s^(n-1) ) + b ( alpa t^(n-2) + beta s^(n-2) )
or
t^2 alpha t^(n-2) + s^2 beta s^(n-2) = a ( t alpa t^(n-2) + s beta s^(n-2) ) + b ( alpa t^(n-2) + beta s^(n-2) )
This equation holds for all n such that you can derive an equation for t and s.
Plugging in the results in the above equations gives you the non-recursive solution.
Try to reproduce it and then go for the actual task.
Cheers.
I've writen this code for this quiz:
"Create a function that computes the approximation of pi, based on the number of iterations specified."
pi can be computed by 4*(1-1/3+1/5-1/7+1/9- ...).
it gives me correct answers in pycharm. but when I run it in the website it gives me wrong answers. what should I do?
thanks
def piApprox(num):
i = 1
pi = 0
while i <= num:
a = (4/((2*i)-1))*((-1)**(i-1))
pi += a
print(pi)
i += 1
return round(pi, 11)
correct answers:.................my answers:
3.13959265559...................3.0
4.0.......................................4.0
3.04183961893...................3.0
3.25236593472...................3.0
To me it looks like the round function isn't working as expected. Maybe just remove it so
return pi
and see if that makes a difference.
I haven't had to do much Math in my code since leaving the university, so I decided to do a refresher, using the book called "Mathematics and Physics for Programmers".
The book says, on page 113, that to rotate a point around the origin, the formula is (angles are in radian):
new_x = sqrt(x*x + y*y) * cos(alpha - atan(y,x))
new_y = sqrt(x*x + y*y) * sin(alpha - atan(y,x))
Sounds simple, but is different from all the examples I saw in Google about rotating. When Googling, it seems that everyone else does it like this:
new_x = x * cos(angle) - y * sin(angle)
new_y = y * cos(angle) + x * sin(angle)
(Which seems to be giving correct results)
Now my problem is that it doesn't work, and I'd like to know why. My assumption is that using an angle (alpha) near 0.0, there should be hardly any changes in the coordinates, but what I get is that the sign of some coordinate components are negated.
For example, (-3.333, -1.667) turns to (-3.333, 1.667) with a rotation of 0.004 radian.
The Java code looks like this:
double h = sqrt(x*x + y*y);
double atanyx = atan2(y,x);
double angle = alpha - atanyx;
return new Point(h*cos(angle), h*sin(angle));
So what is the problem? Is the formula wrong in that book? If not, why is it different from what everyone else does? And why is my Java implementation not working as expected? My expectation being that an angle near 0 would cause negligible changes in the coordinates.
The formula in the book is wrong. It should be:
new_x = sqrt(x*x + y*y) * cos(atan(y,x) + alpha)
new_y = sqrt(x*x + y*y) * sin(atan(y,x) + alpha)
For what it's worth, the second formula you have above is both faster and much more commonly used.