Math, something a bit complicated for me - math

so I have a math question, i need to use it in my code.
say this:
630 ---> 10000
125 ---> 1000
230 ---> ??
can anyone help me solve this?
with an explanation maybe...

It looks like multiplication by 5 on the left translates to multiplication by 10 on the right, as 125*5=625. I'd guess that you do not want a linear but an exponential connection, something like
y=10^log5(x) = pow( 10, log(x)/log(5) )
= x*pow( 2, log(x)/log(5) )
= x*pow(x, log(2)/log(5) )

Related

modelling an infinite series in R

I'm trying to write a code to approximate the following infinite Taylor series from the Theis hydrogeological equation in R.
I'm pretty new to functional programming, so this was a challenge! This is my attempt:
Wu <- function(u, repeats = 100) {
result <- numeric(repeats)
for (i in seq_along(result)){
result[i] <- -((-u)^i)/(i * factorial(i))
}
return(sum(result) - log(u)-0.5772)
}
I've compared the results with values from a data table available here: https://pubs.usgs.gov/wsp/wsp1536-E/pdf/wsp_1536-E_b.pdf - see below (excuse verbose code - should have made a csv, with hindsight):
Wu_QC <- data.frame(u = c(1.0*10^-15, 4.1*10^-14,9.9*10^-13, 7.0*10^-12, 3.7*10^-11,
2.3*10^-10, 6.8*10^-9, 5.7*10^-8, 8.4*10^-7, 6.3*10^-6,
3.1*10^-5, 7.4*10^-4, 5.1*10^-3, 2.9*10^-2,8.7*10^-1,
4.6,9.90),
Wu_table = c(33.9616, 30.2480, 27.0639, 25.1079, 23.4429,
21.6157, 18.2291, 16.1030, 13.4126, 11.3978,
9.8043,6.6324, 4.7064,2.9920,0.2742,
0.001841,0.000004637))
Wu_QC$rep_100 <- Wu(Wu_QC$u,100)
The good news is the formula gives identical results for repeats = 50, 100, 150 and 170 (so I've just given you the 100 version above). The bad news is that, while the function performs well for u < ~10^-3, it goes off the rails and gives negative outputs for numbers within an order of magnitude or so of 1. This doesn't happen when I just call the function on an individual number. i.e:
> Wu(4.6)
[1] 0.001856671
Which is the correct answer to 2sf.
Can anyone spot what I've done wrong and/or suggest a better way to code this equation? I think the problem is something to do with my for loop and/or an issue with the factorials generating infinite numbers as u gets larger, but I'm not at all certain.
Thanks!
As it says on page 93 of your reference, W is also known as the exponential integral. See also here.
Then, e.g., the package expint provides a function to compute W(u):
library(expint)
expint(10^(-8))
# [1] 17.84347
expint(4.6)
# [1] 0.001841006
where the results are exactly as in your referred table.
You can write a function that takes in a value together with the repetition times and outputs the required value:
w=function(u,l){
a=2:l
-0.5772-log(u)+u+sum(u^(a)*rep(c(-1,1),length=l-1)/(a)/factorial(a))
}
transform(Wu_QC,new=Vectorize(w)(u,170))
u Wu_table new
1 1.0e-15 3.39616e+01 3.396158e+01
2 4.1e-14 3.02480e+01 3.024800e+01
3 9.9e-13 2.70639e+01 2.706387e+01
4 7.0e-12 2.51079e+01 2.510791e+01
5 3.7e-11 2.34429e+01 2.344290e+01
6 2.3e-10 2.16157e+01 2.161574e+01
7 6.8e-09 1.82291e+01 1.822914e+01
8 5.7e-08 1.61030e+01 1.610301e+01
9 8.4e-07 1.34126e+01 1.341266e+01
10 6.3e-06 1.13978e+01 1.139777e+01
11 3.1e-05 9.80430e+00 9.804354e+00
12 7.4e-04 6.63240e+00 6.632400e+00
13 5.1e-03 4.70640e+00 4.706408e+00
14 2.9e-02 2.99200e+00 2.992051e+00
15 8.7e-01 2.74200e-01 2.741930e-01
16 4.6e+00 1.84100e-03 1.856671e-03
17 9.9e+00 4.63700e-06 2.030179e-05
As the numbers become large the estimation is not quite good, so we should have to go further than 170! but R cannot do that. Maybe you can try other platforms. ie Python
I think I may have solved this myself (though borrowing heavily from Onyambo's answer!) Here's my code:
well_func2 <- function (u, l = 100) {
result <- numeric(length(u))
a <- 2:l
for(i in seq_along(u)){
result[i] <- -0.5772-log(u[i])+u[i]+sum(u[i]^(a)*rep(c(-1,1),length=l-1)/(a)/factorial(a))
}
return(result)
}
As far as I can tell so far, this matches the tabulated results well for u <5 (as did Onyambo's code), and it also gives the same result for vector vs single-value inputs.
Still needs a bit more testing, and there's probably a tidier way to code it using map() or similar instead of the for loop, but I'm happy enough for now. Thought I'd share in case anyone else has the same problem.

Finding any one solution to two inequalities in 2 variables (Python)

I have two inequalities as below and while I can find the appropriate regions for which the inequalities hold true, I am looking for any one particular value rather than solving the inequality by hand. So one of the solution in this case would be when d=-4.5 and g=3. And I want any one correct solution. I am using sage.
Input: solve([-1/2*d + 1/2 < g, g < -d - 1],[d,g])
Output: [[-2*g + 1 < d, d < -g - 1, 2 < g]]
The closest I have got is by using sympy modules but I was not able to solve 2 equations, only 1 works:
Input: solve_univariate_inequality(x**2 >= 4, x, relational=False)
Output:(−∞,−2]∪[2,∞)
Is anyone aware of a technique that would lead me to a solution?
I'm confused as to why the Sage answer isn't acceptable. It says any g>2 so pick g=4, maybe, and then any d between -2*g+1 and -g-1, so that would be between -7 and -5, so -6 works. The point is to give you all answers.
You could do something like this, but you'd still have to look at the first solution to see what was appropriate for d, or find a way to programmatically extract that information - and that might be challenging, since in general your inequality may not have a nice solution like this, and the order of the inequalities in the solution might have a more random order.
var('d,g')
S = solve([-1/2*d + 1/2 < g, g < -d - 1],[d,g])[0]
[s.subs(g=4) for s in S]
I hope this at least helps you frame your question, if not perhaps even answer your actual query; good luck.

Introduction to programming using SML exercise 1.3

I am currently working through the Introduction to programming using SML book released back in 1999. I would like your help on how to declare a recursive function for exercise 1.3, which I have been stuck on for some time now.
1.3: Declare a recursive function f: int -> int, where
f(ᶯ) = 1 + 2 + ... + (ᶯ - 1) + ᶯ for ᶯ ≥ 0
Hint: Use two clauses with 0 and ᶯ as argument patterns. State the recursion formula corresponding to the declaration.
If anyone can provide some guidance on this that would be great.
More literal hint:
The solution is supposed to look like this:
fun sum 0 = ... something ...
| sum n = ... something else ...
You'll need to figure out
What is sum 0?
What information do you need in order to be able to calculate sum n?
If you're stuck, write down the calculation of f(1), f(2), f(3), and f(4) on a piece of paper and see if you can spot a pattern.

Merkle Hellman Knapsack cryptosystem

I am working on a java problem that implements Merkle Hellman's Knapsack. The wikipedia page is http://en.wikipedia.org/wiki/Merkle%E2%80%93Hellman_knapsack_cryptosystem.
After testing with some simple sample data, some of them are successful while others are not. For example,
input = 'f'; (01100110)
Encryption:
w = ( 1,2,4,7,12,20,33,54)
r = 147
q = 250
b = (147,44,88,29,14,190,101,188)
r-1(reverse) = 233 (r*r-1 mod q =1)
The cryptogram is therefore 423 (=44+88+190+101)
Decryption:
Then 423 * 233 mod 250 = 59
59-54=5
5-4=1
1-1=0
THE RESULT is 10100001. But it is WRONG!
I have checked it numerous times and just cannot find which step is wrong with my process. Also, I am aware that the numbers I use should be random. Here, I just want to give an example.
Could anyone shed some lights on this?
Many thanks!
Your w is not superincreasing.
There are conditions for Merkle-Hellman you should follow in order for your output to be correct, here are they:
the simple knapsack must be super-increasing
the weight(multiplier) must be greater in value than the sum of your simple knapsack
the modulus and weight values mustn't have a common factor

Finding global maximum in matlab

Can any body know how to find the global maximum of a signal in matlab.
Any help will be highly appreciated.
Thanks
assuming your signal is a vector x, just do
[max_value, index_number] = max(x)
max_value will be the biggest value and index_number will be the index number of your original vector x.
the biggest peak is not always the maximum it can always be the first or last element in the vector (if of curse you really mean the biggest peak and not maximum of function )
[~,indexes]=findpeaks(x,'SORTSTR','descend');
i=indexes(1);
This is a very bad question as not enough information has been provided by the OP. but fminbnd() might be a good option to look into:
clear; close all; clc;
myFun = #(x) -min(sin(x), x^2);
[x1, y1] = fminbnd(myFun, -1, 2);
You can use isoutlier function like this example
A = [57 59 60 100 59 58 57 58 300 61 62 60 62 58 57];
TF = isoutlier(A)
If you want only specific parts, you can divide your vector as isoutlier(A(5:25)) or similar

Resources