Mathematical modelling in Matlab - matlab

I am new to Matlab and I need to perform a simple task. I have an input function u(t)=2t-1 and an output function
y(t)=(-4/45)e^3t+ (1/5)e^(-2t)-1/3t-1/9
I have to find the corresponding behavior model, defined via a transfer function and then simulate the behavior model in Matlab/Simulink. I am not sure what approach I should use. What I have done so far is that I transferred the input and output function in the s-domain and then I calculated the transfer function. What does it mean to simulate the behavior model in Matlab? Can anyone please help me? I am a very beginner.
Thank you very much!

If you've already done the Laplace transform and calculated the transfer function in s-domain, I think it means applying the transfer function to an arbitrary transient input and calculating the response.
The transfer function is based on the response due to a unit step function.
If you have an input that is something other than a unit step function (e.g. a sinusoid), you'd calculate the response as follows:
Transform the arbitrary input function to s-space.
Multiply it by the transfer function to the response in s-space.
Transform the response in s-space back to the time domain.
This is how Laplace transforms work.

Related

How to compute derivative of a neural network model at a particular point using MATLAB?

In using neural networks for solving differential equations, using MATLAB, I could find derivative of the model using the command 'dlgradient'. i.e.
gradientsU = dlgradient(sum(U,'all'),{dlX},'EnableHigherDerivatives',true)
where U is the model found using fullyconnect and dlX is the input.
Now, how we can calculate derivative at a particular point of the model U.
To be specific I wanted to add derivative of the model at a particular point, say U_0=U'(5) to the loss function. So, how can I compute that?
I have followed the documentation given in MATLAB, given by,
"To evaluate Rosenbrock's function and its gradient at the point [–1,2], create a dlarray of the point and then call dlfeval on the function handle #rosenbrock.
x0 = dlarray([-1,2]);
[fval,gradval] = dlfeval(#rosenbrock,x0);
But I have already called using dlfeval to evaluate the model, so, not able to call dlfeval again. And when I'm trying to directly compute using the command dlfeval(#U,U_0), the output is always zero.
It will be really helpful if some insight could be provided. Thanks in advance.

Sample sequence of points from continuous signal simulink

I have a Matlab function (created by me) that must be evaluated only at a given rate. I would like to sample the value of a signal, give to this function (discrete values) and then, the calculated output must be hold until the next value is available. Is there a way in simulink to do this? All answers I have found use quantizer + ZOH but in this case I still get "a continuum" (or almost it) of points to be evaluated by thsi function which is really slow. Changing the rate of simulink's solver is also not an option as the result of this function will be given for a continuous time system.
Any help will be highly appreciatted!
Thank you
Assuming by Matlab function you mean a MATLAB Function block, then it sounds as if all you need to do is make the block discrete. Do that by right-clicking on the block, going down to Block Properties and then in the resulting dialog enter your required sample time.
The block will then sample its input and generate an output (which is held between sample times) at each sample time.

Using System Identification Toolbox transfer function with Simulink

I believe I am doing something fundamentally wrong when trying to import and test a transfer function in Simulink which was created within the System Identification Toolbox (SIT).
To give a simple example of what I am doing.
I have an input which is an offset sinusoidal wave from 12 seconds to 25 seconds with an amplitude of 1 and a frequency of 1.5rad/s which gives a measured output.
I have used SIT to create a simple 2 pole 1 zero transfer function which gives the following agreement:
I have then tried to import this transfer function into Simulink for investigation in the following configuration which has a sinusoidal input of frequency 1.5rad/s and a starting t=12. The LTI system block refers to the transfer function variable within the workspace:
When I run this simulation for 13 seconds the input to the block is as expected but the post transfer function signal shows little agreement with what would be expected and is an order of magnitude out.
pre:
post:
Could someone give any insight into where I am going wrong and why the output from the tf in simulink shows little resemblance to the model output displayed in the SIT. I have a basic grasp of control theory but I am struggling to make sense of this.
This could be due to different initial conditions used in SimuLink and the SI Toolbox, the latter should estimate initial conditions with the model, while Simulink does nothing special with initial conditions unless you specify them yourself.
To me it seems that your original signals are in periodic regime, since your output looks almost like a sine wave as well. In periodic regime, initial conditions have little effect. You can verify my assumption by simulating your model for a longer amount of time: if at the end, your signal reaches the right amplitude and phase lag as in your data, you will know that the initial conditions were wrong.
In any case, you can get the estimated initial state from the toolbox, I think using the InitialState property of the resulting object.
Another thing that might go wrong, is the time discretization that you use in Simulink in case you estimated a continuous time model (one in the Laplace variable s, not in z or q).
edit: In that case I would recommend you check what Simulink uses to discretize your CT model, by using c2d in MATLAB and a setup like the one below in Simulink. In MATLAB you can also "simulate" the response to a CT model using lsim, where you have to specify a discretization method.
This set-up allows you to load in a CT model and a discretized variant (in this case a state-space representation). By comparing the signals, you can see whether the discretization method you use is the same one that SimuLink uses (this depends on the integration method you set in the settings).

How Do I Use fft() Function In Matlab To Find The Frequency For A Set Of Data Points?

I've been given a set of about 20k data points. I managed to import the data into Matlab and did the following :
importdata;
fft(importdata);
and it returns :
Undefined function 'fft' for input arguments of type 'cell'
Now, I understand that I need more than this to get it working.
Can someone please tell me any more parameters I need for the fft and how to implement it?
Edit: These datapoints are timestamps of when something is detected in a machine,
I'm trying to find if there is a period of the detection occuring.
Look at the documentation for the correct way to use importdata and fft. Both have examples to get you started.
First you need to assign the output of importdata to a matrix, then do any data manipulation you need to get it in a form to be used by fft. Finally, use fft with the correct parameters and compute the correct frequency vector as per the example (obviously, adapted to your data)

Designing a Matlab filter from a Laplace equation

I have a Laplace Transfer function that I wish to use in a high pass filter. I am trying to design a filter inside Matlab that will use this function using FDAtool but I don't understand how the parameters and coefficients this tool uses relates to the Laplace function I have.
Can anyone provide some information or links regarding the relationship between a function and the filter that uses it.
Why don't you simply use a Transfer Fcn block (or a Discrete Transfer Fcn block if it's discrete) since you already have the Laplace transfer function? FDAtool is for designing a filter as far as I know. It sounds like your filter is already designed.