Solving heat equation in pipe - simulation

I want to simulate heat transfer from a hot fluid flowing through a pipe into a surrounding solid. The temperature of the fluid entering the pipe is varying.
The best thing I've thought of so far, is using variable which stores its previous value (old) and manually shifting values along the pipe plus assigning current inlet fluid temperature to the volume in the beginning of the pipe to simulate flow while using short enough time intervals to avoid discontinuities.
While this might work, there are noticeable drawbacks since mesh elements have different volume, so assigning other value means the stored energy is changed which is not good for a simulation and since the flow is always in the same direction, this error might accumulate.
I'm wondering if there is a better way of simulating this. Any suggestions?
Thank you!

If I understand the physics you're trying to include in your simulation, you want steady flow in the pipe with a variable inlet temperature and then heat diffusion in the entire domain, pipe and surrounding fluid, correct?
If so, I would model convection-diffusion over the entire domain, with a convection coefficient corresponding to the fluid velocity in the pipe and zero elsewhere. I'd set a Variable constraint on the pipe inlet, outflow constraint on the pipe outlet, and (default) no flux or Dirichlet everywhere else.

Related

Iteratively display values during optimization process in MATLAB

I am using lsqnonlin in MATLAB to align a rigid skeleton model to raw joint center data recorded from a device that does not necessarily yield a rigid skeleton. Therefore, my objective function to be minimized is the distance between model joint locations and raw data joint locations.
My algorithm does a pretty good job (red is the rigid model and blue is the raw data):
except for some select frames where my skeleton is off:
I am interested in examining the solution process graphically so that I can visualize what lsqnonlin is trying to do for every frame. Ideally, I would output a History but this seems to be only available for other solvers.
For lsqnonlin it seems that I need to specify a plotting function for the PlotFcn option in optimoptions. I have extracted from the optimation Toolbox the function optimplotx.m which displays a bar graph of the state variables during the iteration process as lsqnonlin converges. My plan is to tweak this function to reproduce the plot for the red skeleton previously displayed.
My problem is that the quantity to be optimized is not the joint locations themselves, but joint angles. I have another function which does forward kinematics and gives me joint positions from joint angles. I would like to pass model parameters into optimplotx.m so that I may call my forward kinematics function and get the plot done but I am unsure how to do that.
These are the inputs to optimplotx.m:
It seems that somewhere deep in the Optimization Toolbox, MATLAB expects these inputs, and they are not to be changed. It is required that I call #optimplotx anonymously anyway, without passing in arguments. It seems that the only way I might be able to get around this is to make my model parameters global in both my main file and optimplotx. Is there any way around this?

Converter control simulation on simulink

I am struggling with a little project I decided to tackle. I am trying to replicate an example I found on a book using matlab simulink but I have no experience with simulink and control theory (I do understand the principles etc.).
The control block diagram is given but I do not understand some blocks and how to add my input (sine wave block on simulink)?
Here are the details:
Example I wish to reproduce
Schematic of the converter and desired control block diagram
If anyone could give me a little insight or direct me to some examples from which I could build on an understanding would be great!
Thank you in advance.
The portion entitled controller is the closed-loop feedback control for the system. K(s) would typically contain some type of PI control. In a more complicated control system, the structure of K(s) may be a little different, but will usually always contain an integration in order to ensure that the system eventually settles at the desired value.
The input Iref is your current command. In this case you would inject your sinusoid here which would produce a current waveform matching your desired output. If your desired output.
Output m is the modulating waveform produced by the controller. Everything inside the half-bridge converter section is a representation of the converter and everything that it is interfaced to (voltage sources).
The feedforward filter here is also a very important component. Since Vs contains an alternating waveform, the feed forward filter allows the system to respond to changes in Vs without relying on feedback compensation K(s). This helps to decouple current regulation from changes in voltage VD.
To start with the project, you can probably build the half bridge converter as shown. You can inject 400*cos(377t - pi/2) as VD.
For the feedback compensator K(s) you can feed the input into two gains (Ki and Kp) which you will select values for later. At the output of Ki insert an integrator (1/s) then sum the output of Kp and the integrator together.
For the feed-forward filter, you should probably just use a low pass filter with a gain of 1 at DC. The low pass filter prevents noise from entering the system. In this case you are running a simulation, so there will be no noise. However, the filter will eliminate any algebraic loops, which can cause warnings or errors in the simulation.
You can input your control signal at Iref.

Combination of two signals to determine the end signal sent through Simulink code

Goal
I'm trying to have a second signal (caused by force applied to a sensor's surface area), once it reaches a set minimum, to determine a servo motor's position. The motor's position is otherwise determined by a first signal (generated by EMGs that are measured with electrodes placed on a subject's body) that make give the servo preset positions when the signal crosses a certain threshold.
Schematic
I cannot seem to get the two signals to work together. Any corrections and recommendations on how to make the logic within Simulink function as intended would be immensely appreciated. I know little of the program and cannot find a good approach.
In your diagram you go from numeric values (Sensors) to logical ones (>=301) back to numeric (*90) and again to logical (AND). What you are doing may be possible but hides the intention. First of all I would recommend you to clearly distinguish between logical conditions and values, similar to the following model. To help you with that you can turn on data-type display from Format > Port/signal display > Signal data types followed by an Update (Ctrl-D).
Alternatively you could reduce the conditions as much as possible and operate only with values, such as
However I do not fully understand your requirements and I'm also not sure what the input of the servo is representing (Speed, position, ...)? Maybe you could explain the desired behaviour a bit more detailed? In particular how do you come up with the limits (301, 300, ...).

3rd-order rate limiter in Simulink? How to generate smooth triggered signals?

First for those, who are not familiar with Simulink, there is a imaginable outside-Simulink partial solution:
I need to create a vector satisfying the following conditions:
known initial value a1
known final value a2
it has a pre-defined step size, but the length is not pre-determined
the first derivative over the whole range is limited to v_max resp. -v_max
the second derivative over the whole range is limited to a_max resp. -a_max
the third derivative over the whole range is limited to j_max resp. -j_max
at the first and the final point all derivatives are zero.
Before you ask "what have you tried so far", I just had the idea to solve it outside Simulink and I tried the whole stuff below ;)
But maybe you guys have a good idea, while I keep working on my own solution.
I'd like to generate smooth ramp signals (3rd derivative limited) based on a trigger signal in Simulink.
To get a triggered step I created a triggered subsystem propagating the trigger output. It looks like that:
But I actually don't want a step, I need a very smooth ramp with limited derivatives up to the 3rd order. The math behind is:
displacement: x
speed: v = x'
acceleration: a = v' = x''
jerk: j = a' = v'' = x'''
(If this looks familiar to you, I once had a very similar question. I thought about a bounty on it, but after the necessary edit of the question both answers would have been invalid)
As there are just rate limiters of 1st order, I used two derivates and a double integration to resolve my problem. But there is a mayor drawback, I can not ignore anymore. For the sake of illustration I chose a relatively big step size of 0.1.
The complete minimal example (Fixed Step, stepsize: 0.1, ode4): Download here
It can be seen, that the signal not even reaches the intended step height of 10 and furthermore is not constant at the end.
Over the development process of my whole model, this approach was satisfactory enough for small step sizes. But I reached the point where I really need the smooth ramp as intended. That means I need a finally constant signal at exactly the value, specified by the step height gain.
I already spent days to resolve the problem, and hope to fine some help here now.
Some of my ideas:
dynamically increase the step height over the actual desired value and saturate the final output. If the rate limits,step height and the simulation step size wouldn't be flexible one could probably find a satisfying solution. But as everything has to be flexible, there are too much cases where the acceleration and jerk limit is violated.
I tried to use the Matlab function block and write my own 3rd order rate limiter. Though it seems possible for me for the trigger moment, I have no solution how to smooth the "deceleration" at the end of the ramp. Also I'd need C-compilers, which would make it hard to use my model on other systems without problems. (At least I think so.)
The solver can not be changed siginificantly (either ode3 or ode4) and a fixed step size is mandatory (0.00001 to 0.01).
Currently used, not really useful approach:
For a dynamic amplification of 1.07 I get the following output (all values normalised on their limits):
Though the displacement looks nice, the violation of the acceleration limit is very harmful.
For a dynamic amplification of 1.05 I get the following output (all values normalised on their limits):
The acceleration stays in its boundaries, but the displacement does not reach the intended value. (not really clear in the picture) The jerk is still to big. (I could live with that, but it's not nice)
So it appears to me that a inside-Simulink solutions is far from reality. Any ideas how to create a well-behaving custom function block?
Simulation step size, step height, and the rate limits are known before the simulation starts. (But I have a lot of these triggered smooth ramps in a row, it should feed a event-discrete control). So I could imagine to create the whole smooth ramp outside simulink and save it as a timeseries object and append it on the current signal when the trigger is activated.
The problems you see are because the difference is not conditioned very well.
Taking the difference amplifies the numerical that exists in your simulation.
Also the jerk will always be large if you try to apply an actual step.
I guess for your approach it would be better to work the other way around:
i.e. make a jerk, acceleration and velocity with which your step is achieved.
I think your looking for something like the ref3 block:
http://www.dct.tue.nl/home_of_ref3.htm
Note the disclaimer on the site and that it is a little cumbersome to use.
An easy (yet to be improved) way is to use a rate limiter and then a state space model with a filter. From the filter you get the velocity, which in turn you can apply a rate limiter to. You continue with rate-limiter and filters until you have the desired curve.
Otherwise you can come up with numerical rate-limiters of higher order using ie runge kutta formulas or finite differences. However it was pointed out, that they may suffer from bad conditioning.
What I usually do is to use one rate limiter and a filter of 3rd Order and just tune the time constant (1 tripple pole), such that my needs are met. This works well, esp
Integrator chains of length > 1 are unstable!
There is a huge field of research dealing with trajectory planning. The easiest way might be to use FIR filters (Biagotti et al) or to implement an online trajectory planner (Ezair et al 2014 / Knierim et al 2012).

Rotational mechanical system in Simulink

I'm simulating a shaft system in Simulink, where I have to find the displacement of a mass. I'm not sure how to model this in Simulink because of the shaft and pulley. I'm looking through the documentation and the closest thing I see to a shaft is the wheel and axle block. But the shafts are connected by a flexible shaft, which is similar to a spring. Any ideas?
This is a fairly trivial task when using SimScape, which is especially made to simulate physical systems. You'll find most of the blocks you need ready from the library.
I've used SimScape to create a model of a complete hybrid truck... In Simulink it can be done, but you'll need to build your own differential equations for the task. In your case, the flexible axle could be translated to another block with a spring/damper system inside.
If you haven't got access to SimScape, you may also consider to use .m (matlab) files to write your differential equations. This can then be used as a block in Simulink, varying (only) a few parameters over time.
Take this step by step:
1. Draw a free body diagram, write out equations for all the forces as a function of displacement, velocity and acceleration of every element (including rotation obviously). For instance, you know that force on the box m will be *c*dy/dt* plus whatever the pulley experiences.
2. Sort out the rotation of the rod first. You know that *T=I*d(omega)/dt* if you get rid of the rest of the system. So, do something analogous to the car engine example of MatLab: Divide the input T by I to get the acceleration, integrate it to get velocity and one more time to get rotational displacement.
3. Keep adding bits one by one. First, you know that there will be a moment proportional to k*(theta_1-theta_2) acting. This will oppose the motion of rod 1 and act to create motion of rod 2. Add a new "branch" to your model to get theta_2 same way you got theta_1.
4. Go on including further elements...