I am very new to Simulink.
I want to achieve the following:
if(U1 > U2)
{
E = U1 + U2
D = U1 * A
}
else if(U1 < U2)
{
E = U1 - U2
D = U1 * B
}
else
{
E = U1
D = U2 * U2
}
Could you please help me how to represent the above in Simulink
There are multiple ways you could implement this.
One is to use an If subsystem. On the left of the image below is the high level of the implementation. On the right are the contents of the If, ElseIf and Else subsystems. You probably also want to look at the doc for the Merge block to see how it selects which signal is passed through to be the final value for D and E.
Another would be to use a MATLAB Function block,
Related
I have the hourly electricity demand of a household for a year. I determined the ARIMA order by using auto.arima in R and now I want to use the received ARIMA order to estimate and forecast the next 24 hours in a MATLAB program.
Lets assume the fitted model is of order: p = 2, d = 0, q = 2; P = 2, D = 1, Q = 0 (frequency = 24). So an arima (2,0,2)(2,1,0)[24] model. How do I insert these parameters into the arima function in Matlab? So far i got:
%Create arima model
Mdl = arima(p,d,q);
Mdl.Seasonality = frequency;
%Estimate the coefficients
EstMdl = estimate(Mdl,past_data);
%Run the forecast for the next 24 hours
[yF,yMSE] = forecast(EstMdl,24,'Y0',past_data);
But that does not include the seasonal parameters so far. Indeed, I am quite confused by the documentation provided by mathworks.
Thankful for any suggestions!
Max
I have no experience in matlab, but looking at the documentation for arima it might be possible to specify the model as
c = 0;
p = 2;
d = 0;
q = 2;
S = 24;
P = 2;
D = 1;
Q = 0;
arima('constant', c, ...
'ARLags', p, ...
'D', d, ...
'MALags', q, ...
'Seasonality', D * S, ...
'SARLags', D * P * S) %, ...
% 'SMALags', D * Q * S) %commented seasonal MA because D * Q * S = 0
This is based on the Create SARIMA Model Template example from the link above, and seems to execute properly in their online IDE. Note due to my lack of any experience in matlab, i have not checked the results comparing model estimates. Note as well that 'Seasonality', 'SARLags' and 'SMALags' must be positive integers (or lacking), Otherwise an error seems to be returned.
I am very new to OOP in Matlab. Let's say I have a super class with one property, X. Next I want to define subclasses that has properties V, A, and B, where V is some manipulation of X, with four options:
V = A1*X - B1;
V = A1*X - B2;
V = A2*X - B1;
V = A2*X - B2;
In the actual code the options are something completely different, and it takes quite some time to calculate V, so once it is calculated I definitely want to store it. (In fact my options for A and B are actually discrete choices, so there are really just four possible combinations).
So what I did is: I created a subclass, VClass, with properties A, B, V (and X).
The SuperClass has methods:
function obj = SuperClass(X)
obj.XX = XX;
end
function add_V(obj,A,B)
if A == A1 && B == B1;
obj.A1B1 = VClass();
obj.A1B1.init_V(X,A,B);
elseif A == A2 && B == B1;
obj.A2B1 = VClass();
obj.A2B1.init_V(X,A,B);
et cetera...
end
The VClass has method:
function init_V(obj,X,A,B)
obj.V = A*X - B;
obj.A = A;
obj.B = B;
obj.X = X;
end
The thing that I don't like is that I created subclasses with names A1B1, A1B2, A2B1, and A2B2, so in every method I repeat a lot of code. I suppose I could use 'eval' with creation of a name for the subclass:
if A == A1 && B == B1;
name = 'A1B1';
.....
and in combination with:
eval(['obj.',name,' = VClass();']);
eval(['obj.',name,'.init_V(X,A,B);']);
But I am not too happy about that either, since it makes the code difficult to read.
Does anybody have another suggestion? (and any other comments of course also very welcome, since I just started with OOP)
I'm trying to write a recursive multigrid algorithm in MATLAB.
I need to store the values of v and TC at each of the levels I'm going through to use when I'm no longer in the recursion by just calling them, in a similar way v{L} would have worked for example if I did a for loop.
Any help is much appreciated. Thank you.
function x = vcycle1d(n, x, b, T, w, levels)
x = damped_jacobiM(w, x, T, b, 10^-7, 3);
res = b - T*x;
k = log2(n+1);
N = 2^(k-1)-1;
RE = zeros(N,n);
for i = 1:N
RE(i,2*i-1:2*i+1) = [1 2 1];
end
RE = RE/4;
II = 2*RE';
TC = RE * T * II;
v = zeros(N, 1);
for i = 1:N
v(i) = (res(2*i-1) + 2*res(2*i) + res(2*i+1))/4;
end
if levels ~= 1
err= vcycle1d(N, zeros(length(v), 1), v, TC, w, levels-1);
else
err = TC\v;
end
There are two ways to do this.
Option 1: Use a persistent variable. This allows you to store details in a variable that retains values between calls.
The pros of this are that you can use it to "remember" all sorts of things from previous calls.
The cons of this are that you'll need to manage that memory in some sensible way.
Option 2: Use additional, optional, inputs.
function x = vcycle1d(n, x, b, T, w, levels, varargin)
if nargin == 7
priordetails = varargin{1};
end
currentdetails = ... ;
....
err= vcycle1d(N, zeros(length(v), 1), v, TC, w, levels-1, currentdetails);
In literature, papers and a book (e.g., [1] and [2]), I found different equations for the Izhikevich Neuron model.
In [1], I found this:
v' = 0.04v^2 + 5v + 140 - u + I
u' = a(bv - u)
if v >= 30 mV => v = c; u = u + d
In [2], I found this:
C*v' = k(v - v_r)(v - v_t) - u + I_in
u' = a{b(v - v_r) - u}
if v >= v_peak => v = c; u = u + d
I tried to go from the second set of equations to the first set of equations, ignoring the C. I found this:
k = 0.04; v_r = -82.6556; v_t = -42.3444
However, this gave me complete garbage. Thus, my question is as follows:
Why are there 2 different sets of equations, and which one should I take if I want to simulate Izhikevich neurons?
Bonus question #1: How do I go from set 2 to set 1?
Bonus question #2: Am I correct to state that 'I' is in [pA]?
References:
[1] Simple Model of Spiking Neurons - E.M. Izhikevich (2003)
[2] Dynamical Systems in neuroscience - E.M. Izhikevich (2007)
1) question #1: go from set 2 to set 1:
1.1) create a new variable V:
V = C(v - v_r)
1.2) you obtain:
(v - v_r) = V / C
v = (V / C) + v_r
v' = (V' / C) + A
1.3) replace (1.2) in set 2 and you'll obtain set 1 with new coefficients:
K = k/(C^2) = 0.04
D = (k/C)(v_r - v_t) = 5
E = -CA = 140
B = b/C
V' = KV^2 + DV + E -u +I
u' = a(BV' - u)
2) question #2: is 'I' expressed as [pA]?
yes, it is. v is [mV] and each time step is 1ms
What would be the best way to simplify a function by getting rid of a loop?
function Q = gs(f, a, b)
X(4) = sqrt((3+2*sqrt(6/5))/7);
X(3) = sqrt((3-2*sqrt(6/5))/7);
X(2) = -sqrt((3-2*sqrt(6/5))/7);
X(1) = -sqrt((3+2*sqrt(6/5))/7);
W(4) = (18-sqrt(30))/36;
W(3) = (18+sqrt(30))/36;
W(2) = (18+sqrt(30))/36;
W(1) = (18-sqrt(30))/36;
Q = 0;
for i = 1:4
W(i) = (W(i)*(b-a))/2;
X(i) = ((b-a)*X(i)+(b+a))/2;
Q = Q + W(i) * f(X(i));
end
end
Is there any way to use any vector-like solution instead of a for loop?
sum is your best friend here. Also, declaring some constants and creating vectors is useful:
function Q = gs(f, a, b)
c = sqrt((3+2*sqrt(6/5))/7);
d = sqrt((3-2*sqrt(6/5))/7);
e = (18-sqrt(30))/36;
g = (18+sqrt(30))/36;
X = [-c -d d c];
W = [e g g e];
W = ((b - a) / 2) * W;
X = ((b - a)*X + (b + a)) / 2;
Q = sum(W .* f(X));
end
Note that MATLAB loves to handle element-wise operations, so the key is to replace the for loop at the end with scaling all of the elements in W and X with those scaling factors seen in your loop. In addition, using the element-wise multiplication (.*) is key. This of course assumes that f can handle things in an element-wise fashion. If it doesn't, then there's no way to avoid the for loop.
I would highly recommend you consult the MATLAB tutorial on element-wise operations before you venture onwards on your MATLAB journey: https://www.mathworks.com/help/matlab/matlab_prog/array-vs-matrix-operations.html