I want to insert the below function K, in my simulink model, to calculate the dynamic transmission error (dte).
The problem is the function K depend on Θp. I don't know if there is any way to do that in simulink.
I would appreciate any suggest.
Here you can find the simulink model
Simulink Model (NEW)
/note: Answer has been rewritten completely.
You can't use a gain block. Any parameter like "Gain" is only evaluated once at the start of the simulation. If you have something which changes over time like your Θp you have to use the signal.
In your case having a one line MATLAB expression to evaluate, the easiest way to use it is a "Function Block" (User-Defined Functons->Fcn), not a MATLAB Function as I originally suggested. Replace the MATLAB Function you already got in your code with a Function Block and use the code:
a0 + sum( af .* cos(n * zp * u) + bf .* sin(n * zp * u) )
The nice advantage is, that all workspace variables are already initialized.
I applied some modifications to the formula, using element-wise multiplication where I expect it should be used. You can use the same code line in MATLAB to verify it really does what you expect.
Related
I am trying to use the cg! function from IterativeSolvers.jl on for solving matrix-matrix linear systems, i.e. AX=B for A,X,B appropriately sized matrices. Given the way the indices work, X[:,i] is independent of all but B[:,i], and so this actually boils down to n different linear solves. Direct solving via \ works automatically in this case, but iterative solvers like CG don't. I can easily do this with a loop on the outside, but I haven't been able to get the in-place op working. For now, my code looks like this:
for j=1:size(u,2)
u[freenode,j],ch = cg!(u[freenode,j],lhs,Dinv.*rhs(u,i)[:,j]) # Requires Vector, need to change rhs
end
which gives solves CG with the appropriate lefthand side and righthand side. But the reason why it's not in-place boils down to this simple example throwing an error:
using IterativeSolvers
y = view(ones(4,2),:,2)
A=rand(4,4)
cg!(y,A,view(zeros(4,2),:,2))
which is:
ERROR: MethodError: no method matching init!
(::IterativeSolvers.KrylovSubspace{Float64,Array{Float64,2}}, ::SubArray{Float64,1,Array{Float64,2},Tuple{Colon,Int64},true})
Closest candidates are:
init!{T}(::IterativeSolvers.KrylovSubspace{T,OpT}, ::Array{T,1}) at C:\Users\Chris\.julia\v0.5\IterativeSolvers\src\krylov.jl:66
in #cg!#23 at C:\Users\Chris\.julia\v0.5\IterativeSolvers\src\cg.jl:7 [inlined]
in cg!(::SubArray{Float64,1,Array{Float64,2},Tuple{Colon,Int64},true}, ::Array{Float64,2}, ::SubArray{Float64,1,Array{Float64,2},Tuple{Colon,Int64},true}, ::Int64) at C:\Users\Chris\.julia\v0.5\IterativeSolvers\src\cg.jl:6 (repeats 2 times)
The problem seems to not be with the views, given the results of a previous SE question
I have doubts that you'll be able to avoid allocations, because the init! function is implemented as
function init!{T}(K::KrylovSubspace{T}, v::Vector{T})
# K.v = Vector{T}[all(v.==zero(T)) ? v : v/norm(v)]
K.v = Vector{T}[copy(v)]
end
and hence there's a copy anyway. Nevertheless, if you want this function to accept views, it should not be an issue to just modify the Vector to AbstractVector. (The function is simple enough that if you don't like to modify the package, you can just add a more general method yourself.)
You're right, it's not a "view" problem per se, the problem seems to be that the init! method seems to not have been defined in a way that can accept a 'view' (i.e. a SubArray type). The error suggests that "the closest candidate is"
init!{T}(::IterativeSolvers.KrylovSubspace{T,OpT}, ::Array{T,1})
(i.e. there's no method definition for a SubArray, only for a normal Array, and it appears there's no generic function / base case available to fall back on)
If you 'collect' the array first, then it works (presumably) as expected:
julia> cg!( collect(y), A, view(zeros(4,2),:,2))
([0.0752658,-0.693794,0.330172,0.437474],IterativeSolvers.ConvergenceHistory{Float64,Array{Float64,1}}(false,0.0,5,[0.249856,0.392572,0.401496,0.463142]))
except obviously that's of no use to you because you don't really get to change y as intended this way.
The only way I see around it if you're intent on keeping y as a view until that point, is to temporarily 'hack' it into a normal array inside cg! using a compound statement:
cg!((y = collect(y); y),A,view(zeros(4,2),:,2))
but, obviously, it's no longer a view at this point, so you'd have to update the original array it was a view into manually ...
I'm looking to integrate a function I am building, but the function would change each iteration based on a given input. For instance:
y=4e^(mx/4)
I would want to integrate with respect to x with a lower and upper bound, but the value of m would change. I know all my values of m.
Can I work with this? My initial assumption would be to use QROMB but that seems limited and unable to handle my issue.
QROMB (and other integrators) want a function of one variable, so you have to get the m in there through the back door. One way is with a common block:
function integrand,x
common int_common,int_m
return,4*exp(int_m*x/4)
end
function integrator,m,xlow,xhigh
common int_common,int_m
int_m=m
return,qromb('integrand',xlow,xhigh)
end
integrator(m,xlow,xhigh) will return the integral you want.
I am new to using Halide and I am playing around with implementing algorithms first. I am trying to write a function which, depending on the value of the 8 pixels around it, either skips to the next pixel or does some processing and then moves on to the next pixel. When trying to write this I get the following compiler error:
84:5: error: value of type 'Halide::Expr' is not contextually convertible to 'bool'
if(input(x,y) > 0)
I have done all the tutorials and have seen that the select function is an option, but is there a way to either compare the values of a function or store them somewhere?
I also may be thinking about this problem wrong or might not be implementing it with the right "Halide mindset", so any suggestions would be great. Thank you in advance for everything!
The underlying issue here is that, although they are syntactically interleaved, and Halide code is constructed by running C++ code, Halide code is not C++ code and vice versa. Halide code is entirely defined by the Halide::* data structures you build up inside Funcs. if is a C control flow construct; you can use it to conditionally build different Halide programs, but you can't use it inside the logic of the Halide program (inside an Expr/Func). select is to Halide (an Expr which conditionally evaluates to one of two values) as if/else is to C (a statement which conditionally executes one of two sub-statements).
Rest assured, you're hardly alone in having this confusion early on. I want to write a tutorial specifically addressing how to think about staged programming inside Halide.
Until then, the short, "how do I do what I want" answer is as you suspected and as Khouri pointed out: use a select.
Since you've provided no code other than the one line, I'm assuming input is a Func and both x and y are Vars. If so, the result of input(x,y) is an Expr that you cannot evaluate with an if, as the error message indicates.
For the scenario that you describe, you might have something like this:
Var x, y;
Func input; input(x,y) = ...;
Func output; output(x,y) = select
// examine surrounding values
( input(x-1,y-1) > 0
&& input(x+0,y-1) > 0
&& ...
&& input(x+1,y+1) > 0
// true case
, ( input(x-1,y-1)
+ input(x+0,y-1)
+ ...
+ input(x+1,y+1)
) / 8
// false case
, input(x,y)
);
Working in Halide definitely requires a different mindset. You have to think in a more mathematical form. That is, a statement of a(x,y) = b(x,y) will be enforced for all cases of x and y.
Algorithm and scheduling should be separate, although the algorithm may need to be tweaked to allow for better scheduling.
Hello,I need a function as random in C language. Maybe you will say that i can call C function, but the effect is not the same in visual c++ tool. So, I need your help.
thanks.
See the Noise library:
https://github.com/DLR-SR/Noise
It has some models and functions to generate random numbers.
If you are using Dymola, you can use the function rand():
model rand_model
Real a(start=rand());
Real b(start=rand());
equation
when (sample(1,1)) then
a = rand();
b = rand();
end when;
end rand_model;
The function is not documented in the Dymola user manual and it is no part of the modelica standard. The output seems to be an integer between 0 and 32767, seed seems to be constant.
Perhaps the implementation is given in the moutil.c file which is shipped with Dymola. But i'm not sure.
I would like to define an object/symbol in mathematica which will have several parameters, for example, something like: S=(1-t)({b_i}^x,{b_i}^y)+t({b_{i+1}}^x,{b_{i+1}}^y) (sort of LaTeX notation). In the example I'm trying to describe the line segment connecting two points b_i and b_{i+1}.
How can I define such an object in mathematica?
I found the following two questions:
Mathematica Notation and syntax mods
Subscripted variables
But I'm not sure that I am using them correctly. What I have done is the following. First I invoked:
Needs["Notation`"];
Symbolize[
ParsedBoxWrapper[
SubscriptBox["\[SelectionPlaceholder]", "\[Placeholder]"]]]
Symbolize[
ParsedBoxWrapper[
SuperscriptBox["\[SelectionPlaceholder]", "\[Placeholder]"]]]
Then, I actually defined the object:
(1 - t) {Subscript[b, i]^x, Subscript[b, i]^y} +
t {Subscript[b, i + 1]^x, Subscript[b, i + 1]^y}
Is this the right way to do what I want?
I'm not entirely sure I understand what you want. The 'object' you are talking about
(1 - t) {Subscript[b, i]^x, Subscript[b, i]^y} +
t {Subscript[b, i + 1]^x, Subscript[b, i + 1]^y}
isn't really a single entity, but the sum of two lists each consisting of two components. So, I assume that you really want to define Subscript[b, i]^x as a symbol.
You can do this with Symbolize from the Notation package. It's absolutely crucial, though, that you use the template generated when you press the Symbolize button on the Notation palette (you get this upon running << Notation`). Then enter your compound variable. I'll be assuming the superscript x and y are fixed symbols and the subscript is are variable:
One more thing:
It might not be a good idea to use Subscript[b, i]^y as you'd lose the ability to raise subscripted variables to the power x and y (a minor loss, but still). Instead you might want to use Subsuperscript[b,i,y]. Please note that the sentence in the 'More information' part of the Subsuperscript documentation page seems to be patently wrong. It says:
To enter a subsuperscript in a notebook, use either Ctrl+_ to begin a
regular subscript or Ctrl+^ to begin a regular superscript. After
typing the first script, use Ctrl+% to move to the opposite script
position. Ctrl+Space moves out of the subscript or superscript
position.
If you do a FullForm on the resulting object you'll see you have made a Subscript[b, i]^y instead. To get the symbol for pasting in the Symbolize template I see no other solution than typing Subsuperscript[b, i_, y], evaluating, and copying the result to the template.
Further to Sjoerd's answer: Since you say the symbol S takes various parameters, I think you might want to explore theSetDelayed method to define a function with parameters. Assumuing that you do want S to be vector-valued with two points, then something like the following would define S in the way you want it.
S[x_,y_,t_,i_]:= (1-t) * {b[i]^x,b[i]^y} + t * {(b[i+1])^x,(b[i+1])^y}
The question then is whether you really need the subscripting. Sjoerd's answer shows how this is done using the Notation package, but you should consider whether this additional complication is necessary to your analysis.
EDIT in response to rcollyer's very helpful comment
You can use Format to define a TraditionalForm representations for a custom function. This is similar to defining UpValues, but relates to the representation rather than the re-writing rules.
Something like the following should work:
Format[S[x_,y_,t_,i_],TraditionalForm] :=
(1 - t) {Subscript[b, i]^x, Subscript[b, i]^y} +
t {Subscript[b, i + 1]^x, Subscript[b, i + 1]^y}