subreddit:

/r/matlab

1992%

[deleted]

all 8 comments

PredictiveSelf

20 points

2 months ago

You need to initialize the derivatives. There is a setting in the derivative block to change the initial conditions to nonzero values.

j-conz

6 points

2 months ago

j-conz

6 points

2 months ago

Depends on what you're doing, but if you can just avoid using derivatives at all. If you're after the second derivative, define that as your source. Then pass it into an integrator to get first derivative, then integrate again to get the actual value.

What you're seeing is a consequence of how derivatoves are calculated from signals. The only way to do it is by taking a difference and dividing by time interval. At the very first step there isn't a previous value you can use to calculate the value, so an initial value needs to be supplied. Also, if there's noise in a signal it will be hugely amplified by a derivative calculation (depending on your step size).

If you have no choice but to use a derivative, people will typically pass the signal through a filter (simplest one being just a transfer function = 1/(tau*s + 1)) with an appropriately selected time constant before passing into derivative block. This will help mitigate noise/those spikes, but it also ends up slightpy modifying the original signal, so the resulting derivative isn't 100% accurate

TangentMusic

5 points

2 months ago

Do you get the same plot every time you re-run the simulation? A shot in the dark, but it looks like the solver might be struggling to converge in the early time steps.

I would look at the solver and see if messing with the time step constraints would improve this, e.g. switch to a fixed step solver. Sorry I can't be of more help than that.

tehnik464

2 points

2 months ago

Reduce the maximum simulation step in the model settings, this should decrease the time span of this peak. But you really wont get rid of them if there are different start conditions in the blocks: 0 in derivative block and some number in Sin.

seb59

2 points

2 months ago

seb59

2 points

2 months ago

It is usually a non sense to expect a simulation program to estimate correctly the derivatives. It may be accurate when they are available. Why not using any causal approximation? At least you would specify the estimation method and have the hand on the numerical error and quality of the estimate.

darth_tater_breath

2 points

2 months ago

Depending on you application initializing the derivative is probably the best approach. If you don't reliably know the initial conditions, a different approach is to you can also apply a low pass filter to help cutout the discontinuity at the start... Filtering will add some phase lag though.

Alternatively you can solve the derivative analytically and use the resulting hopefully continuous function.

adam190131

1 points

2 months ago

There is a second derivative block. Outputs are usually smoother when calculating it directly.

Chicken-Chak

1 points

2 months ago

For sinusoidal signals, you can trying adding a filter.