create a vector with size x and value f(x) in octave - octave

I have to generate a dataset of N data points, which are defined as t_n=f(x_n)+e, where e is drawn from normal distribution and f(x) is a nonlinear function.
For example, i have a function f(x)=x²+2x+10, how can i fill a vector v, such:
x = 1:1:100;
v = create(f(x)+normrnd(0,1),x);
Thank you

There are many methods to do this. Here I show you how to do it with anonymous functions http://www.gnu.org/software/octave/doc/v4.0.1/Anonymous-Functions.html#Anonymous-Functions
f = #(x) polyval ([1 2 10], x)
x = 1:100;
v = f(x) + normrnd (0, 1, size (x));
Or without a function:
x = 1:100;
v = x.^2 + 2.*x + 10 + normrnd (0, 1, size (x));
I've adjusted x here so that the noise is visible:
x = linspace (-3, 3);
v = f(x) + normrnd (0, 1, size (x));
plot (x, v)
grid on

Related

How can gauss newton method implemented using armijo line search in python?

We define the sigmoidal function
σ(t) = 1 / (1+e−t)
It has the derivative σ′(t) = σ(t)(1 − σ(t)). The module gauss_newton contains a function generate_data(gamma=0) which generates a data set (ti , αi ) where ti ∈ R and αi ∈ R with
αi = σ(6ti + 1) + εiγ.
for i = 1, . . . , 10. The values εi ∼ N (0, 1) are independently normally distributed and the real value γ ∈ R controls the influence of εi.
(i) Solve the problem min (1/2(∥F(x)∥^2),
with Fi(x) = σ(x1ti + x2) − αi for i = 1,...,10 and γ = 0 using the Gauss Newton algorithm . Iterate until the size of the search direction is sufficiently small, i.e. until ∥∆xk ∥ < δ for some tolerance δ > 0.

How to plot Iterations in Julia

I coded a function picircle() that estimates pi.
Now I would like to plot this function for N values.
function Plotpi()
p = 100 # precision of π
N = 5
for i in 1:N
picircle(p)
end
end
3.2238805970149254
3.044776119402985
3.1641791044776117
3.1243781094527363
3.084577114427861
Now I am not sure how to plot the function, I tried plot(PP()) but it didn't work
Here I defined picircle:
function picircle(n)
n = n
L = 2n+1
x = range(-1, 1, length=L)
y = rand(L)
center = (0,0)
radius = 1
n_in_circle = 0
for i in 1:L
if norm((x[i], y[i]) .- center) < radius
n_in_circle += 1
end
end
println(4 * n_in_circle / L)
end
Your problem is that your functions don't actually return anything:
julia> x = Plotpi()
3.263681592039801
3.0646766169154227
2.845771144278607
3.18407960199005
3.044776119402985
julia> x
julia> typeof(x)
Nothing
The numbers you see are just printed to the REPL, and print doesn't return any value:
julia> x = print(5)
5
julia> typeof(x)
Nothing
So you probably just want to change your function so that it returns what you want to plot:
julia> function picircle(n)
n = n
L = 2n+1
x = range(-1, 1, length=L)
y = rand(L)
center = (0,0)
radius = 1
n_in_circle = 0
for i in 1:L
if norm((x[i], y[i]) .- center) < radius
n_in_circle += 1
end
end
4 * n_in_circle / L
end
Then:
julia> x = picircle(100)
3.263681592039801
julia> x
3.263681592039801
So now the value of the function is actually returned (rather than just printed to the console). You don't really need a separate function if you just want to do this multiple times and plot the results, a comprehension will do. Here's an example comparing the variability of the estimate with 100 draws vs 50 draws:
julia> using Plots
julia> histogram([picircle(100) for _ ∈ 1:1_000], label = "100 draws", alpha = 0.5)
julia> histogram!([picircle(20) for _ ∈ 1:1_000], label = "20 draws", alpha = 0.5)

Interpolation in n dimensions

I'm trying to use interpn function and probing even a simple 2D example I cannot reproduce the interpolation value FUN(xi,yi) compared to the matrix grid value i.e. FUN(1,11) != 4.
A = [13,-1,12;5,4,3;1,6,2];
x = [0,1,2];
y = [10,11,12];
xi = linspace (min (x), max (x), 30);
yi = linspace (min (y), max (y), 60);
FUN = interpn (x, y, A, xi, yi, "spline");
disp("value(1,11) = "), FUN(1, 11)

multi-sensors fusion using Kalman filter

I need to use the Kalman filter to fuse multi-sensors positions for gaussian measurement (for example 4 positions as the input of the filter and 1 position as output). It is possible to help me with some examples or tutorials because all the examples I found are related to the estimation of the positions?
OPTION 1
Weighted Avarage
In this case you don't need to implement a real Kalman Filter. You just can use the signal variances to calculate the weights and then calculate the weighted avarage of the inputs. The weights can be found as an inverse of the variances.
So if you have two signals S1 and S2 with variances V1 and V2, then the fused result would be
A fusion example can be seen on the next plot.
I simulated two signals. The variance of the second signal changes over the time. As long as it's smaller than the variance of the first signal the fused result is close to the second signal. It is not the case when the variance of the second signal is too high.
OPTION 2
Kalman Filter with Multiple Update Steps
The classical Kalman Filter uses prediction and update steps in a loop:
prediction
update
prediction
update
...
In your case you have 4 independent measurements, so you can use those readings after each other in separate update steps:
prediction
update 1
update 2
update 3
update 4
prediction
update 1
...
A very nice point is that the order of those updates does not matter! You can use updates 1,2,3,4 or 3,2,4,1. In both cases you should get the same fused output.
Compared to the first option you have following pros:
You have a variance propogation
You have the system noise matrix Q,
so you can control the smoothness of the fused output
Here is my matlab code:
function [] = main()
% time step
dt = 0.01;
t=(0:dt:2)';
n = numel(t);
%ground truth
signal = sin(t)+t;
% state matrix
X = zeros(2,1);
% covariance matrix
P = zeros(2,2);
% kalman filter output through the whole time
X_arr = zeros(n, 2);
% system noise
Q = [0.04 0;
0 1];
% transition matrix
F = [1 dt;
0 1];
% observation matrix
H = [1 0];
% variance of signal 1
s1_var = 0.08*ones(size(t));
s1 = generate_signal(signal, s1_var);
% variance of signal 2
s2_var = 0.01*(cos(8*t)+10*t);
s2 = generate_signal(signal, s2_var);
% variance of signal 3
s3_var = 0.02*(sin(2*t)+2);
s3 = generate_signal(signal, s3_var);
% variance of signal 4
s4_var = 0.06*ones(size(t));
s4 = generate_signal(signal, s4_var);
% fusion
for i = 1:n
if (i == 1)
[X, P] = init_kalman(X, s1(i, 1)); % initialize the state using the 1st sensor
else
[X, P] = prediction(X, P, Q, F);
[X, P] = update(X, P, s1(i, 1), s1(i, 2), H);
[X, P] = update(X, P, s2(i, 1), s2(i, 2), H);
[X, P] = update(X, P, s3(i, 1), s3(i, 2), H);
[X, P] = update(X, P, s4(i, 1), s4(i, 2), H);
end
X_arr(i, :) = X;
end
plot(t, signal, 'LineWidth', 4);
hold on;
plot(t, s1(:, 1), '--', 'LineWidth', 1);
plot(t, s2(:, 1), '--', 'LineWidth', 1);
plot(t, s3(:, 1), '--', 'LineWidth', 1);
plot(t, s4(:, 1), '--', 'LineWidth', 1);
plot(t, X_arr(:, 1), 'LineWidth', 4);
hold off;
grid on;
legend('Ground Truth', 'Sensor Input 1', 'Sensor Input 2', 'Sensor Input 3', 'Sensor Input 4', 'Fused Output');
end
function [s] = generate_signal(signal, var)
noise = randn(size(signal)).*sqrt(var);
s(:, 1) = signal + noise;
s(:, 2) = var;
end
function [X, P] = init_kalman(X, y)
X(1,1) = y;
X(2,1) = 0;
P = [100 0;
0 300];
end
function [X, P] = prediction(X, P, Q, F)
X = F*X;
P = F*P*F' + Q;
end
function [X, P] = update(X, P, y, R, H)
Inn = y - H*X;
S = H*P*H' + R;
K = P*H'/S;
X = X + K*Inn;
P = P - K*H*P;
end
And here is the result:

Prolog, predicate that swaps 1 to 0 and 0 to 1 and puts them in a list

New to Prolog.
I want a program that swaps 1 to 0 and 0 to 1 and answers that question:
?- swap([1,1,0,1,0,0,0,1], L2).
L2 = [0,0,1,0,1,1,1,0]
complement(0, 1).
complement(1, 0).
swap(X, Y) :- maplist(complement, X, Y).
A CLPFD solution that works properly in all directions, for reference:
:- use_module(library(clpfd)).
swap(X, Y) :-
maplist(#\=, X, Y),
X ins 0..1,
Y ins 0..1.
Specifically:
?- swap(X,Y).
X = Y, Y = [] ;
X = [_2640],
Y = [_2658],
_2640 in 0..1,
_2640#\=_2658,
_2658 in 0..1 ;
X = [_3200, _3206],
Y = [_3224, _3230],
_3200 in 0..1,
_3200#\=_3224,
_3224 in 0..1,
_3206 in 0..1,
_3206#\=_3230,
_3230 in 0..1 ;
…
works properly, when swap/2 from the other answer does not:
?- swap2(X,Y).
X = Y, Y = [] ;
X = [0],
Y = [1] ;
X = [0, 0], % Doesn't enumerate ([1],[0]), etc.
Y = [1, 1] ;
X = [0, 0, 0],
Y = [1, 1, 1] ;
…