Why changing a value doesn't change the Scipy Spline value? - scipy

I have the following code trying to interpolate air temperature values
import numpy as np
from scipy.interpolate import RectBivariateSpline as spline
x = np.array([-7.52, -7.50, -7.48, -7.46])
y = np.array([41.88, 41.90, 41.92, 41.94])
z = np.array([
[1.2, 1.1, 1.5, 1.3],
[1.3, 1.4, 1.5, 1.1],
[1.2, 1.1, 1.2, 1.6],
[1.3, 1.1, 1.2, 1.5]
])
f = spline(x, y, z)
When I call the function with f.ev(41.89, -7.46) the value remains the same [ 1.3] even when I change the values of z. Could you help me?

Are you creating a new f object after changing the values of z? Evidently f is using a copy of z, not z itself. You need to update the evaluator the see the effects of changing z.
I could add a demonstration if needed.
In [373]: x = np.array([-7.52, -7.50, -7.48, -7.46])
In [374]: y = np.array([41.88, 41.90, 41.92, 41.94])
In [375]: z = np.array([
.....: [1.2, 1.1, 1.5, 1.3],
.....: [1.3, 1.4, 1.5, 1.1],
.....: [1.2, 1.1, 1.2, 1.6],
.....: [1.3, 1.1, 1.2, 1.5]
.....: ])
In [376]: f=interpolate.RectBivariateSpline(x,y,z)
In [378]: f(41.89,-7.46)
Out[378]: array([[ 1.3]])
In [379]: f.ev(41.89,-7.46)
Out[379]: array(1.3)
In [381]: z+=2
In [382]: z
Out[382]:
array([[ 3.2, 3.1, 3.5, 3.3],
[ 3.3, 3.4, 3.5, 3.1],
[ 3.2, 3.1, 3.2, 3.6],
[ 3.3, 3.1, 3.2, 3.5]])
In [383]: f.ev(41.89,-7.46)
Out[383]: array(1.3)
In [384]: f=interpolate.RectBivariateSpline(x,y,z)
In [385]: f.ev(41.89,-7.46)
Out[385]: array(3.3000000000000003)

Related

Looping over combinations of several variables for Design Of Experiments in MATLAB

I need to loop over all the possible combinations of the different elements of a vector between lower and upper bounds.
NUMBEROFVARIABLES stores the total number of variables.
tlb < t < tub and o1b < o < oub
The elements t and o can vary in steps of tStep and oStep respectively.
The vector is: x = [t1, t2, t3, ... tn, o1, o2, o3, ... on]
For 1 < t < 10, 1.5 < o < 5, tStep = 1, oStep = 0.5 and NUMBEROFVARIABLES = 10 (divided equally/half-half between 't's and 'o's), the generated combination vectors should be something like:
x1 = [1, 1, 1, 1, 1, 1.5, 1.5, 1.5, 1.5, 1.5]
x2 = [1, 1, 1, 1, 1, 1.5, 1.5, 1.5, 1.5, 2.0]
x3 = [1, 1, 1, 1, 1, 1.5, 1.5, 1.5, 1.5, 2.5]
.
.
.
xi = [1, 1, 1, 1, 2, 5.0, 5.0, 5.0, 5.0, 5.0]
xii = [1, 1, 1, 1, 3, 5.0, 5.0, 5.0, 5.0, 5.0]
xiii = [1, 1, 1, 1, 4, 5.0, 5.0, 5.0, 5.0, 5.0]
.
.
.
xn = [10, 10, 10, 10, 10, 5.0, 5.0, 5.0, 5.0, 5.0]]
How do I loop over each parameter (like in the above example) and store the vectors xi in MATLAB?

fminsearch for non linear regression Matlab?

Can anyone explain to me how I can apply fminsearch to this equation to find the value of K (Diode Equality Factor) using the Matlab command window.
I = 10^-9(exp(38.68V/k)-1)
I have data values as follows:
Voltage := [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]:
Current:= [0, 0, 0, 0, 0, 0, 0, 0.07, 0.92, 12.02, 158.29]:
I used fminsearch and an error message appeared:
Matrix dimensions must agree.
Error in #(k)sum((I(:)-Imodel(V(:),k)).^2)
Error in fminsearch (line 189)
fv(:,1) = funfcn(x,varargin{:});
I used this fminsearch code:
V = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0];
I = [0, 0, 0, 0, 0, 0, 0.07 ,0.92 ,12.02 ,158.29];
Imodel = #(V,k) 1E-9*(exp(38.68*V/k)-1);
k0 = 1;
kmodel = fminsearch(#(k) sum((I(:)-Imodel(V(:),k)).^2), k0)
Please explain what the problem in this code is?
It looks like you are carrying on from this post: Fminsearch Matlab (Non Linear Regression ). The linked post is trying to find the right coefficient k in your equation that minimizes the sum of squared errors between the input, which is predicted current from the current-voltage relation of a diode and the output, which is the measured current from a diode. This current post is simply trying to get that off the ground.
In any case, this is a very simple error. You're missing an element in your current array I. It's missing one 0. You can verify this by using numel on both V and I. Basically, V and I don't match in size. numel(V) == 11 and numel(I) == 10.
The definition you have at the top of your question compared to how you defined your error, it's missing one final zero:
I = [0, 0, 0, 0, 0, 0, 0, 0.07, 0.92, 12.02, 158.29];
%// ^
When I run the code with this new I, I get:
>> kmodel
kmodel =
1.4999

Fminsearch Matlab (Non Linear Regression )

Can anyone explain to me how I can apply non linear regression to this equation t find out K using the matlab command window.
I = 10^-9(exp(38.68V/k)-1).
Screenshot of Equation
I have data values as follows:
Voltage := [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]:
Current:= [0, 0, 0, 0, 0, 0, 0, 0.07, 0.92, 12.02, 158.29]:
Screenshot of Equation
[NEW]: Now I used FminSearch as an alternative another and another error message appeared.
Matrix dimensions must agree.
Error in #(k)sum((I(:)-Imodel(V(:),k)).^2)
Error in fminsearch (line 189)
fv(:,1) = funfcn(x,varargin{:});
I used this fminsearch code:
>> V = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0];
>> I = [0, 0, 0, 0, 0, 0, 0.07 ,0.92 ,12.02 ,158.29];
>> Imodel = #(V,k) 1E-9*(exp(38.68*V/k)-1);
>> k0 = 1;
>> kmodel = fminsearch(#(k) sum((I(:)-Imodel(V(:),k)).^2), k0)
>> kmodel = fminsearch(#(k) sum((I(:)-Imodel(V(:),k)).^2), k0);
You want to find the parameter k that will minimize the sum of squared error of your exponential model (BTW, is that a current/voltage characteristic?) given the current data I and voltage data V as vectors:
Imodel = #(V,k) 1E-9*(exp(38.68*V/k)-1);
k0 = 1;
kmodel = fminsearch(#(k) sum((I(:)-Imodel(V(:),k)).^2), k0);
plot(V(:), I(:), 'ok', V(:), Imodel(V(:),kmodel), '-r');
The anonymous function calculates the sum of squared error. The search for the parameter k that will minimize the model error starts with the value 1; please change it to a more appropriate value (if you have a good guess for it).

Maple converting vectors to data to plot

I have a list of vectors, like this:
{x = 7, y = 0.}, {x = 2.5, y = 0.}, {x = -2.3, y = 0.}, {x = 2.5, y = 2.7}, {x = 2.5, y = -2.7}
How do I convert these to data I can plot? I've been trying with the "convert" function, but can't get it to work.
When I manually convert it to something like [[7, 0], [2.5, 0], [-2.3, 0], [2.5, 2.7], [2.5, -2.7]] it works, though there has to be an automatic way, right?
A little more info about what I'm doing if you're interested:
I have a function U(x,y), of which I calculate the gradient and then check where it becomes 0, like this:
solve(convert(Gradient(U(x, y), [x, y]), set), {x, y});
that gives me my list of points. Now I would like to plot these points on a graph.
Thanks!
S:={x = 7, y = 0.}, {x = 2.5, y = 0.}, {x = -2.3, y = 0.},
{x = 2.5, y = 2.7}, {x = 2.5, y = -2.7}:
T:=map2(eval,[x,y],[S]);
[[7, 0.], [2.5, 0.], [-2.3, 0.], [2.5, 2.7], [2.5, -2.7]]

2d interpolation table (Matlab)

I have a table( w, alfa, eta ):
w = [0, 0.5, 1]
alfa = [0, 0.3, 0.6, 0.9]
eta(0,0.3) = 0.23
eta(0.5,0) = 0.18
eta(0.5,0.6) = 0.65
eta(1,0.9) = 0.47
where, eta = f(w,alfa)
How can I interpolate the data to obtain all values ​​in this table?
I try griddata, interp2 etc but i can't do it.
It seems like griddata should do the work in your case. However, you should notice that your inputs requires extrapolation as well as interpolation.
>> [xout yout] = meshgrid( w, alfa ); % output points
>> w_in = [ 0, 0.5, 0.5, 1 ];
>> a_in = [ 0.3, 0, 0.6, 0.9 ];
>> e_in = [ 0.23, 0.18, 0.65, 0.47 ];
>> eta_out = griddata( w_in, a_in, e_in, xout, yout, 'linear' )