I have a Simulink simulation that takes a control input U on an Inport, and simulates the state of the system based on that input. I want the simulation to use a variable time-step ode solver, but U is going to be defined as discrete time points (that aren't evenly spaced) that certainly aren't going to align with the times that are generated by (say) ode15s.
I want Simulink to take the U vector and a time vector, and use cubic spline interpolation to determine the value of U for times that do not align with the given U vector--similar to the 'Interpolate Data' option in the Inport preferences, except again my data is neither evenly spaced, nor do I want linear interpolation. How can I do this?
A possible way to achieve this is the following. I am assuming the U vector is already known beforehand. This is implied from the fact the vector values are given at random sample moments which are not matching with the solver sample moments.
Take an '1-D interpolation table' block and connect a 'Clock' block as input. In the 1-D interpolation table you are able to specify 'Table data' in your case the values of the U vector. And you are able to specify the breakpoints that are in your case the time points. These can be variables from your workspace.
Then under the tabled 'Algorithm' you choose 'Cubic spline' for the interpolation method.
That should do the trick.
Related
I am designing a battery model with an internal resistance which is dependant on two variables: SoC and temperature.
I have interpolated the data I have (x,y and z basically - a total of 131 points each) with MATLAB's curve fitting toolbox and was able to generate the desired 3D map of that dependence (see the picture below):
My question is how can I use that map now for my Simulink model? As input parameters I will have SoC and temperature and the resistance in ohm should be the output. However, I have not been able to find a convenient way to export the data in a suitable lookup table (or similarly useful, my first guess was that I should use a 2-D lookup table in this case) in Simulink. However, I am quite new to this and I do not know how to generate the the table data for the Simulink LUT.
Simulink LUT:
Table data is your interpolated z-data from curve fitting. I guess it will have a value for every combination of breakpoints (i.e. it covers every grid intersection in your first diagram). So if Breakpoint 1 is 100 elements and Breakpoint 2 is 40 elements, Table data is 100x40.
If you can't get the data out from the GUI-based interactive curve fit, I guess you can extract the data from the command line. The following is an excerpt of Mathworks' curve fitting documentation. It would be good to verify this because I don't have the toolbox to test it though.
•Interpolation: fittedmodel = fit([Time,Temperature], Energy, 'cubicinterp');
•Evaluation: fittedmodel(80, 40)
Based on your LUT inputs u1 and u2, the table will interpolate or extrapolate the grid to get your output value.
Hope that helps.
I did find a solution after all, thanks Tom for your help, the fittedmodel() function was indeed the key of it. I then used two FOR loops to populate my matrix which was 49x51 (as seen by the grid in the image) after the cftool interpolation. After that it was all a matter of two for loops in one another to populate my matrix with the z values of my T and SoC parameters.
for x = 1:49
for y = 1:51
TableData(x,y)=fittedmodel(B_SoC(x),B_Temp(y));
end
end
Where TableData is the 49x51 matrix required for my LUT, B_SoC and B_Temp being [0:2.083:100] and [-10:1.1:45] respectively (determined as the desired start and end of my x and y axis with the spacing taken from the image with the data cursor).
I have a system of three nonlinear equations with eight unknowns. I'm currently setting each equation equal to a desired value and then using Matlab's fsolve (a numerical solver) to find a solution. Instead of running fsolve in real-time, I'd like to pre-compute solutions for a specific set of values to which I set the equations equal.
Pursuant that goal, I've run the solver over a set of values and created a 3D matrix (N x N x N) which I've attempted to load into eight Simulink 3-D lookup tables, Direct Lookup Table n-D block, so I can fetch each of the eight solved unknowns. It's my understanding the inputs to this block should work the same way I would reference an element in my 3-D array: table(x,y,z) but I'm constantly getting Simulink table input out-of-range errors. I've confirmed the inputs are within the table size, so I'm not sure what's wrong.
This isn't the most elegant implementation, so I'm open to better solutions. Ideally, I'd like to have a Simulink lookup that takes three inputs and returns a vector of the eight solved unknowns, or even better, can do some type of linear interpolation between the three lookup values to return an approximate solution.
Thanks!
First I describe the physics, it is in a axisymmetric space, one sound source was placed at the original point, one sensor was placed on the axis under the source. Giving the source wave form, I try to get the sensor's waveform. all materiel parameter were known, for instance, sound speed, density.
I write the Matlab script to calculate it, by solving the sound propagation equation I can get
one function, say, A(w,k), w is frequency and k is wavenumber, this is so called frequency-wavenumber field. My matlab code like this,
discrete w and k, get a A array. first use FFT to k, get space and frequency information
then, FFT to w, get space and time information, that is the waveform at different point.
the fake code
for i_w=...
w=...
for i_k=...
k=...
M=A(w,k)
end
wave_space_freq=ifft(M)
end % here can specify the only point of the sensor
wave_space_freq=ifft(wave_space_freq)
My question is do I need to make conjugation and flip when I use IFFT,like ifft(M,0,fliplr(conj(M))) . because I saw some-others use them, but I don't understand why?
If you want a strictly real-valued result waveform (not complex with significant imaginary components), then the input to an IFFT has to be conjugate symmetric, such as:
ifft(dc_term,M,0,fliplr(conj(M))).
I am trying to do a 'convolution' of an arbitrary N-dimensional surface with a vector. More specifically, I am trying to get the output of an N-th order Volterra kernel (see http://www.scholarpedia.org/article/Volterra_and_Wiener_series, eq.1)
Thus, for a 1-dimensional kernel, the output is the simple sliding convolution of the 1st order kernel with the past input epoch. It essentially multiplies/weights every value in the past input epoch with a coefficient.
The 2nd order kernel output is a 'convolution' of a 2d matrix with a signal. This kernel weighs the product of every pair of points in the past with a coefficient.
The 3rd order kernel, a 3d matrix, weighs every triplet of points in the past memory epoch.
Also, I dont know the dimension/order of the kernel before hand. It is an input parameter..
I know I can probably do this very unelegantly and slowly by going through with several for loops point by point, but I was wondering if there was a way to do this very quickly and elegantly in matlab?
Thanks
you can check the dimension of a variable with ndims(array), and do convolutions on the needed dimensions: conv() or conv2() for one-dimension and 2-dimensions respectively. If you need to do a convolution on a higher dimension, you can reshape your variable with the reshape command and then use the previous commands.
I need to plot concentration from two instruments so I can compare the results. Ineed to plot concentration from instrument (A) on one axis and concentration from another instrument (B) on the other. The problem is instrument A has a time of 1 hour where as instrument B has 1 minute.
How would I select a specific time where the instrument A can be plotted.
figure
plot(averagetime,averageconcentration_A),'color','r');
hold on
plot(num,(B_concentration_in_mgperlitre);
datetick('x')
ylabel 'Average Concentration(mg/litre)';
xlabel 'Average Time';
This plots a line plot against time.
Perform linear interpolation to get data points for instrument A as if it also had a time of 1 minute.
see: http://en.wikipedia.org/wiki/Linear_interpolation
Perform interpolation on your data with bigger interval. There are interpolation functions in Malab. However, you need to be careful about the method. If your data moves linearly then you will be able to get a good result as your time stamped are consistent.
I think by looking at your data you would judge the linear propagation of the points. If you are not sure about the function that fits to your data use curve-fitting toolbox to find the best match. Then you need to interpolate according to that function within two points. Curvefitting can be applied reginoally. I mean you don't need to fit a curve for the whole data, finding the best match between t consecutive points will suit your purpose.
In case you want to use linear interpolation, use Matlab function interp1
And this lib lininterp1f