How do I make a three-dimensional plot correctly in Scilab? - plot

I am trying to make a plot of three column vectors right now in order to make a three dimensional surface plot, but the output is just a discontinuous line. Any help is appreciated, code is below. Apologies in advance for the confusing variable names.
co = 29;
BF = .0446;
WPW = 50;
E = [0:0.01:2]; //sets up a column vector
p = [];
WBR =[];
w = [];
t = 8.64E13;
delta = [0:0.5:100];
R =[];
DeltaMu = [];
Total = [];
//begin program iteration through k; change "k" value in for loop to change
//the number of iterations the program runs over, and thus the amount
//of degrees of freedom
k = 200;
for i = 1:k,
I = 12.5 + 0.167*i;
mu = I/co;
sigma = .11*mu;
cdf = cdfnor("PQ", E, mu*ones(E), sigma*ones(E));
n = 201; // variable over which the loop is iterated
pdf = zeros(201,1); // sets up an appendable matrix of all zeros
temp = 0; //initiates a temporary integer variable
while n > 1,
temp = cdf(n) - cdf(n-1); //assigns a value to the temp variable every pass
pdf(n) = temp; //assigns the temp value to the nth slot in the
//column vector, works through the matrix backwards
n = n-1; //iterates the while loop on n
end //breaks from while loop after n hits 1
temp = cdf(n);
pdf(n) = temp;
n = 201;
while n > 1,
a = exp(-WPW*exp(-delta(n)*(1-E)));
n = n-1;
end
n = 201; // variable over which the loop is iterated
prob = zeros(201,1); // sets up an appendable matrix of all zeros
temp = 0; //initiates a temporary integer variable
while n > 1,
temp = a(n)*pdf(n); //assigns a value to the temp variable every pass
prob(n) = temp; //assigns the temp value to the nth slot in the
//column vector, works through the matrix backwards
n = n-1; //iterates the while loop on n
end //breaks from while loop after n hits 1
WBR(i) = sum(prob)*BF
w(i) = mu
end
//begin program iteration through k; change "k" value in for loop to change
//the number of iterations the program runs over, and thus the amount
//of degrees of freedom
k = 200;
for i = 1:k,
mu = .5*i;
sigma = .1*mu;
cdf = cdfnor("PQ", delta, mu*ones(delta), sigma*ones(delta));
n = 201; // variable over which the loop is iterated
pdf = zeros(201,1); // sets up an appendable matrix of all zeros
temp = 0; //initiates a temporary integer variable
while n > 1,
temp = cdf(n) - cdf(n-1); //assigns a value to the temp variable every pass
pdf(n) = temp; //assigns the temp value to the nth slot in the
//column vector, works through the matrix backwards
n = n-1; //iterates the while loop on n
end //breaks from while loop after n hits 1
temp = cdf(n);
p = 1-(exp(-t*exp(-delta)));
n = 201; // variable over which the loop is iterated
Psw = zeros(201,1); // sets up an appendable matrix of all zeros
temp = 0; //initiates a temporary integer variable
while n > 1,
temp = p(n)*pdf(n); //assigns a value to the temp variable every pass
Psw(n) = temp; //assigns the temp value to the nth slot in the
//column vector, works through the matrix backwards
n = n-1; //iterates the while loop on n
end //breaks from while loop after n hits 1
R(i) = sum(Psw)
DeltaMu(i) = mu
end
n = 200;
while n > 1,
Total(n) = WBR(n) + R(n);
n = n-1;
end
xdel(winsid()); //close any open graphs
plot3d(WBR, R, Total)

To plot a surface with plot3d, you need:
a vector of x-values
a vector of y-values
a matrix of z-values, where the (i,j) entry will determine the height over the point (x(i),y(j))
Toy example:
plot3d([1 2 3], [2 3 4], [0 1 2; 2 3 2; 0 2 1])
There is no mathematically reasonable to make a surface plot from three column vectors. What you could do with them is draw a parametric curve, which uses three vectors for x,y,z coordinates:
param3d(WBR, R, Total)
With your data, the result is still unspectacular because of the high dynamic range with the arrays. Consider plotting on the logarithmic scale.

Related

Count the sum of subsets of size k when the sum is (Greater than or equal to R) or (Lesser than or equal to L)

def knapSack(A,L,R,K,N,Sum):
if(K==0):
if((L>=Sum)or(R<=Sum)):
return 1
else:
return 0
if((N==0)and(K!=0)):
return 0
else:
return knapSack(A,L,R,K,N-1,Sum)+knapSack(A,L,R,K-1,N-1,Sum:=Sum+A[N-1])
A = [2,4,10,25]
K = 2
L = 3
R = 13
Sum=0
n = len(A)
print(knapSack(A,L,R,K,n,Sum))
The Output Of this Code is: 4
Explanation:
25+10 =35
25+4 = 29
25+2 = 27
10+4 = 14
These Sums satisfies the given condition if((L>=Sum)or(R<=Sum)) where L=3 R=13
K is the size of the subset. Here, K = 2
When K = 3
A = [2,4,10,25]
K = 3
L = 3
R = 13
Sum = 0
n = len(A)
print(knapSack(A,L,R,K,n,Sum))
The Output Of this Code when K = 3 is: 4
Explanation:
4+10+25 = 39
2+4+25 = 31
2+10+25 = 37
2+4+10 = 16
These Sums satisfies the given condition if((L>=Sum)or(R<=Sum)) where L=3 R=13
Is There a way to solve this problem in Dynamic Programming or any other better way?
Here is a dynamic programming algorithm you can use. The algorithm will be used to generate all subsets of size K. This algorithm uses one element of A one after another to build subsets.
STEPS
Initialize a list with the empty set. Also initialize a global_counter which will be used to keep count of subsets of size K that satisfies the given condition.
Iterate the elements of A. For each A[i], do the following;
i. Create new subsets by appending A[i] to all the current elements in the list (Notice that list is currently containing the subsets created using A[0] to A[i-1]).
ii. For each of the new subsets created in 2.i using A[i], let sum be the sum of the elements in the subset, and count be the number of elements in the subset. If count == K AND (sum <= L OR sum >= R) then increment the global_counter. If count < K, insert the newly created subset into the list.
Return the global_counter.
NB: Instead of storing every intermediate subset as a list of elements, we will use (sum, count) pairs to represent subsets. Where sum represent the sum of elements in the subset, while count represent the number of elements in the subset. This way we can reduce the memory required to store all the created subsets and the time required to compute the sum and count each time a subset is created, since sum and count of the a newly created subset can be updated in constant time from the its previous subset.
Time Complexity: O(2^N) - Notice that at worst case, K = N
import java.util.ArrayList;
import java.util.List;
public class SubsetK {
public static void main(String[] args) {
int[] A = {2,4,10,25};
int L = 3;
int R = 13;
int K = 3;
System.out.println("K: "+K+", Result: "+countSubSets(A,K,L,R));
}
private static int countSubSets(int[] A, int K, int L, int R){
//create array to hold subsets of size 0,1,...,(K-1)
//since we are only interested in the sum of a subset, a subset will be
//represented as (sum, count) pairs. Where sum is the sum of elements in the
//subset, and count is the number of elements in the subset.
List<int[]> dp = new ArrayList<>(); // list of [sum,count] pairs
//initialize the array with the empty set
dp.add(new int[]{0,0});
int global_count = 0;
for (int ele : A) {
int size = dp.size();
for (int j = 0; j < size; j++) {
int sum = dp.get(j)[0] + ele;
int count = dp.get(j)[1] + 1;
//check if condition is satisfied
if (count == K && (sum <= L || sum >= R))
global_count++;
//we only store subsets of size 0,..,(K-1)
//since they will be used to build bigger subsets
if (count < K)
dp.add(new int[]{sum, count});
}
}
return global_count;
}
}

Compute product of large 3-D arrays in R

I am working on an optimization problem, and to supply the analytic gradient to the routine, I need to compute the gradient of large 3D arrays with respect to parameters. The largest of these arrays s are of dimensions [L,N,J] where L,J ~ 2000, and N= 15. L and N stand for nodes over which the arrays are then aggregated up with some fixed weights w to vectors of length J. Computing the gradient naively generates a [L,N,J,J] arrays x whose elements are x(l,n,j,k) = -s(l,n,j)s(l,n,k) if j=/=k and x(l,n,j,j) = s(l,n,j)(1-s(l,n,j)).
Several functions in the procedure would use x as input, but as of right now I cannot keep x in memory due to its size. My approach so far has been to compute and directly aggregate up x over L and N to only ever store JxJ matrices, but the downside is that I cannot reuse x in other functions. This is what the following code does:
arma::mat agg_dsnode_ddelta_v3(arma::cube s_lnj,
arma::mat w_ln,
arma::vec w_l){
// Normal Matrix dimensions
unsigned int L = s_lnj.n_rows;
unsigned int N = s_lnj.n_cols;
unsigned int J = s_lnj.n_slices;
//resulting matrix
arma::mat ds_ddelta_jj = arma::mat(J,J, arma::fill::zeros);
for (unsigned int l = 0; l < L; l++) {
for (unsigned int n = 0; n < N; n++) {
arma::vec s_j = s_lnj.subcube(arma::span(l), arma::span(n), arma::span());
ds_ddelta_jj += - arma::kron(w_l(l) * w_ln(l,n) * s_j, s_j.as_row()) + arma::diagmat(w_l(l) * w_ln(l,n) * s_j);
}
}
return ds_ddelta_jj;
}
Alternatively, the 4-D array x could for instance be computed with sparseMatrix, but this approach does not scale up when the L and J increase
library(Matrix)
L = 2
N = 3
J = 4
s_lnj <- array(rnorm(L*N*J), dim=c(L,N,J))
## create spare Matrix with s(l,n,:) vertically on the diagonal
As_lnj = A = sparseMatrix(i=c(1:(L*N*J)),j=rep(1:(L*N), each=J),x= as.vector(aperm(s_lnj, c(3, 1, 2))))
## create spare Matrix with s(l,n,:) horizontally on the diagonal
Bs_lnj = sparseMatrix(i=rep(1:(L*N), each=J),j=c(1:(L*N*J)),x= as.vector(aperm(s_lnj, c(3, 1, 2))))
## create spare Matrix with s(l,n,:) diagonnally
Cs_lnj = sparseMatrix(i=c(1:(L*N*J)),j=c(1:(L*N*J)),x= as.vector(aperm(s_lnj, c(3, 1, 2))))
## compute 4-D array with sparseMatrix product
x = -(As_lnj %*% Bs_lnj) + Cs_lnj
I was wondering if you knew of faster way to implement the first code, or alternatively of an approach that would make the second one scalable.
Thank you in advance

Continued fractions and Pell's equation - numerical issues

Mathematical background
Continued fractions are a way to represent numbers (rational or not), with a basic recursion formula to calculate it. Given a number r, we define r[0]=r and have:
for n in range(0..N):
a[n] = floor(r[n])
if r[n] == [an]: break
r[n+1] = 1 / (r[n]-a[n])
where a is the final representation. We can also define a series of convergents by
h[-2,-1] = [0, 1]
k[-2, -1] = [1, 0]
h[n] = a[n]*h[n-1]+h[n-2]
k[n] = a[n]*k[n-1]+k[n-2]
where h[n]/k[n] converge to r.
Pell's equation is a problem of the form x^2-D*y^2=1 where all numbers are integers and D is not a perfect square in our case. A solution for a given D that minimizes x is given by continued fractions. Basically, for the above equation, it is guaranteed that this (fundamental) solution is x=h[n] and y=k[n] for the lowest n found which solves the equation in the continued fraction expansion of sqrt(D).
Problem
I am failing to get this simple algorithm work for D=61. I first noticed it did not solve Pell's equation for 100 coefficients, so I compared it against Wolfram Alpha's convergents and continued fraction representation and noticed the 20th elements fail - the representation is 3 compared to 4 that I get, yielding different convergents - h[20]=335159612 on Wolfram compared to 425680601 for me.
I tested the code below, two languages (though to be fair, Python is C under the hood I guess), on two systems and get the same result - a diff on loop 20. I'll note that the convergents are still accurate and converge! Why am I getting different results compared to Wolfram Alpha, and is it possible to fix it?
For testing, here's a Python program to solve Pell's equation for D=61, printing first 20 convergents and the continued fraction representation cf (and some extra unneeded fluff):
from math import floor, sqrt # Can use mpmath here as well.
def continued_fraction(D, count=100, thresh=1E-12, verbose=False):
cf = []
h = (0, 1)
k = (1, 0)
r = start = sqrt(D)
initial_count = count
x = (1+thresh+start)*start
y = start
while abs(x/y - start) > thresh and count:
i = int(floor(r))
cf.append(i)
f = r - i
x, y = i*h[-1] + h[-2], i*k[-1] + k[-2]
if verbose is True or verbose == initial_count-count:
print(f'{x}\u00B2-{D}x{y}\u00B2 = {x**2-D*y**2}')
if x**2 - D*y**2 == 1:
print(f'{x}\u00B2-{D}x{y}\u00B2 = {x**2-D*y**2}')
print(cf)
return
count -= 1
r = 1/f
h = (h[1], x)
k = (k[1], y)
print(cf)
raise OverflowError(f"Converged on {x} {y} with count {count} and diff {abs(start-x/y)}!")
continued_fraction(61, count=20, verbose=True, thresh=-1) # We don't want to stop on account of thresh in this example
A c program doing the same:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main() {
long D = 61;
double start = sqrt(D);
long h[] = {0, 1};
long k[] = {1, 0};
int count = 20;
float thresh = 1E-12;
double r = start;
long x = (1+thresh+start)*start;
long y = start;
while(abs(x/(double)y-start) > -1 && count) {
long i = floor(r);
double f = r - i;
x = i * h[1] + h[0];
y = i * k[1] + k[0];
printf("%ld\u00B2-%ldx%ld\u00B2 = %lf\n", x, D, y, x*x-D*y*y);
r = 1/f;
--count;
h[0] = h[1];
h[1] = x;
k[0] = k[1];
k[1] = y;
}
return 0;
}
mpmath, python's multi-precision library can be used. Just be careful that all the important numbers are in mp format.
In the code below, x, y and i are standard multi-precision integers. r and f are multi-precision real numbers. Note that the initial count is set higher than 20.
from mpmath import mp, mpf
mp.dps = 50 # precision in number of decimal digits
def continued_fraction(D, count=22, thresh=mpf(1E-12), verbose=False):
cf = []
h = (0, 1)
k = (1, 0)
r = start = mp.sqrt(D)
initial_count = count
x = 0 # some dummy starting values, they will be overwritten early in the while loop
y = 1
while abs(x/y - start) > thresh and count > 0:
i = int(mp.floor(r))
cf.append(i)
x, y = i*h[-1] + h[-2], i*k[-1] + k[-2]
if verbose or initial_count == count:
print(f'{x}\u00B2-{D}x{y}\u00B2 = {x**2-D*y**2}')
if x**2 - D*y**2 == 1:
print(f'{x}\u00B2-{D}x{y}\u00B2 = {x**2-D*y**2}')
print(cf)
return
count -= 1
f = r - i
r = 1/f
h = (h[1], x)
k = (k[1], y)
print(cf)
raise OverflowError(f"Converged on {x} {y} with count {count} and diff {abs(start-x/y)}!")
continued_fraction(61, count=22, verbose=True, thresh=mpf(1e-100))
Output is similar to wolfram's:
...
335159612²-61x42912791² = 3
1431159437²-61x183241189² = -12
1766319049²-61x226153980² = 1
[7, 1, 4, 3, 1, 2, 2, 1, 3, 4, 1, 14, 1, 4, 3, 1, 2, 2, 1, 3, 4, 1]

Palindrome algorithm in Scialb

Could you help me to construct an algorithm in Scilab that searches for the longest palindrome in a zero-one sequence of n - elements.
The output should be given the length of the palindrome and position in the string starting the searched sequence.
Example: for 111010101100, the longest palindrome is 110101011. The length of the palindrome is 9, and the position in the string starting the sequence is 2.
Here is a possible implementation (for n>1) of the algorithm proposed in the comment above:
x = "10100111000";
n = length(x);
lgmax = 0;
pos = 0;
for i = 1:n-1
// k=0: odd sequence, k=1: even sequence
for k = 0:1
j=1;
while j <= min(i-1+k,n-i) && part(x,i+j) == part(x,i-j+k)
j = j+1;
end
if 2*j-1-k > lgmax
lgmax = 2*j-1-k;
pos = i-j+1+k;
end
end
end
disp(part(x,pos:pos+lgmax-1))

Sum of combinations of numbers

I want to solve a mathematical problem in a fastest possible way.
I have a set of natural numbers between 1 to n, for example {1,2,3,4,n=5} and I want to calculate a formula like this:
s = 1*2*3*4+1*2*3*5+1*2*4*5+1*3*4*5+2*3*4*5
as you can see, each element in the sum is a multiplications of n-1 numbers in the set. For example in (1*2*3*4), 5 is excluded and in (1*2*3*5), 4 is excluded. I know some of the multiplications are repeated, for example (1*2) is repeated in 3 of the multiplications. How can I solve this problem with least number of multiplications.
Sorry for bad English.
Thanks.
Here is a way that does not "cheat" by replacing multiplication with repeated addition or by using division. The idea is to replace your expression with
1*2*3*4 + 5*(1*2*3 + 4*(1*2 + 3*(1 + 2)))
This used 9 multiplications for the numbers 1 through 5. In general I think the multiplication count would be one less than the (n-1)th triangular number, n * (n - 1) / 2 - 1. Here is Python code that stores intermediate factorial values to reduce the number of multiplications to just 6, or in general 2 * n - 4, and the addition count to the same (but half of them are just adding 1):
def f(n):
fact = 1
term = 2
sum = 3
for j in range(2, n):
fact *= j
term = (j + 1) * sum
sum = fact + term
return sum
The only way to find which algorithm is the fastest is to code all of them in one language, and run each using a timer.
The following would be the most straightforward answer.
def f(n):
result = 0
nList = [i+1 for i in range(n)]
for i in range(len(nList)):
result += reduce(lambda x, y: x*y,(nList[:i]+nList[i+1:]))
return result
Walkthrough - use the reduce function to multiply all list's of length n-1 and add to the variable result.
If you just want to minimise the number of multiplications, you can replace all the multiplications by additions, like this:
// Compute 1*2*…*n
mult_all(n):
if n = 1
return 1
res = 0
// by adding 1*2*…*(n-1) an entirety of n times
for i = 1 to n do
res += mult_all(n-1)
return res
// Compute sum of 1*2*…*(i-1)*(i+1)*…*n
sum_of_mult_all_but_one(n):
if n = 1
return 0
// by computing 1*2*…*(n-1) + (sum 1*2*…*(i-1)*(i+1)*…*(n-1))*n
res = mult_all(n-1)
for i = 1 to n do
res += sum_of_mult_all_but_one(n-1)
return res
Here is an answer that would work with javascript. It is not the fastest way because it is not optimized, but it should work if you want to just find the answer.
function combo(n){
var mult = 1;
var sum = 0;
for (var i = 1; i <= n; i++){
mult = 1;
for (var j = 1; j<= n; j++){
if(j != i){
mult = mult*j;
}
}
sum += mult;
}
return (sum);
}
alert(combo(n));

Resources