I'm trying to do a simulation of a 2-body mass-spring-damper. I've set up a state-space model that I'm pretty confident in and set an input of a displacement and velocity at the base in just one degree of freedom. Upon getting my outputs, I expected that the output vector would just be the state vector at each time step. However, when plotting the output vector corresponding to displacement for each mass in the vertical direction (the input direction), it looked much more like a velocity (0 at the extrema of the input). The plots are shown below:
When I integrated the top 2 plots, I got the following:
Now, I obviously can just accept the outputs as they are and assume I am right in my understanding. But, I want to be sure. From the documentation page:
lsim(___) also returns the time vector t used for simulation and the
state trajectories x (for state-space models only)
I'm just hoping to find out whether or not I am correct in that the output matrix columns correspond to the history of the state derivatives before I go base an analysis on a bad assumption.
I figured it out. My B-matrix expected [derivative, state,...] but I had them in the opposite order.
Related
I want to fit the second degree equation to the graph like shows.
I have already "From workspace" blocks and "Scope" blocks
.
Now my problem is how do I fit second degree graph. I have already "constant" block where I have "cars" matrix. Then I have "ramp" block which creates slope between 1-365 like shows.
Then I have "Least Squares Polynomial Fit" and "Polynomial Evaluation" blocks . What I am doing wrong because it isn't working?
This is my matlab code:
n = importdata('n.txt',';')
cars = n(:,2)
trucks = n(:,3)
bus = n(:,4)
t = linspace(1,365,365)
t = transpose(t)
It gives me error "Error in port widths or dimensions. Output port 1 of .. is a [32x3] matrix." and "The number of rows of input A must match the number of rows of input B."
Given the model you show, I assume you have looked at the example for the Least Square Polynomial Fit block, and completely misunderstood what it is doing.
It is showing an example where the coefficients of a polynomial as a function of time are known, then using the Polynomial Evaluation block to evaluate the polynomial, then using the Least Square Polynomial Fit to get the coefficients of the polynomial back again. That is, it is showing that when data is "round tripped" though both blocks you get back to where you started, supposedly giving you confidence that the blocks are "doing the right thing".
But what they show is not what you are wanting to do, and hence you need a different model.
If you look at the documentation for the Polynomial Evaluation block, there are several examples of what the inputs are expected to be.
The first input is the independent variable, which in your case appears as if it should be simulation time. You can use a Ramp for this, but why not just use the Clock block. (If you do use a Ramp then you want it to have an Initial Value of 0, not 365 as you show in the image in your question.)
The second input are the coefficients of the polynomial as a vector. These could be pregenerated (using for instance MATLAB's polyfit) function, or could be generated on the fly using the Least Square Polynomial Fit block.
That is, it looks like you really want the following model:
In MATLAB:
Using the X and Y values below, write a MATLAB function SECOND_DERIV in MATLAB. The output of the function should be the approximate value for the second derivative of the data at x, the input variable of the function.
Use the forward difference method and interpolate to get your final answer;
X=[1,1.2,1.44,1.73,2.07,2.49,2.99,3.58,4.3,5.16,6.19,7.43,8.92,10.7,12.84,15.41,18.49];
Y=[18.89,19.25,19.83,20.71,21.96,23.6,25.56,27.52,28.67,27.2,19.38,-2.05,-50.9,-152.82,-354.73,-741.48,-1465.11];
This is my coding:
function output = SECOND_DERIV(R)
X=[1,1.2,1.44,1.73,2.07,2.49,2.99,3.58,4.3,5.16,6.19,7.43,8.92,10.7,12.84,15.41,18.49];
Y=[18.89,19.25,19.83,20.71,21.96,23.6,25.56,27.52,28.67,27.2,19.38,-2.05,-50.9,-152.82,-354.73,-741.48,-1465.11];
%forward difference method first time.
XX=X(1:end-1)
%first derivative.
dydx=diff(Y)./diff(X)
%second derivative.
dydx2=diff(dydx)
%forward difference method second time.
XXX=XX(1:end-1)
%get the second derivative from input x.
output= interp1(XXX,dydx2,x,'linear','extrap')
end
I do not know what wrong with it.
This is the result I got from my course's web
First, there is no "the" approximate value but rather only "an" approximate value among an infinite set of approximation schemes. In that sense your excercise is ill-defined (but, to be fair, there is probably something you had in the lessons, that completes information).
Using forward differences twice is almost as bad an approximation as it can get. With each forward difference you are displacing the abscissa of the preferred (central difference) approximation by half a sample distance towards the "past".
For the first difference this can be justified by the fact that you might want to stick with the original X-samples. But in the second step you introduce a second displacement by half a sample distance. In order to keep approximation error at least reasonably low, the least you can do is to correct the displacement afterwards by one sample distance towards the "future". This doesn't bring you exactly back to central differences because of non-equidistance, but it's the minimal correction that should be done for the sake of accuracy.
Hence I would replace
XXX=XX(1:end-1)
by
XXX=XX(2:end)
But again, like so many school excercises, the problem is ill-defined and it is difficult to tell from the distance, if this is what is expected from you.
I have a a bunch of data that I'd like to use to find an unknown parameter in a physical equation.
I'm trying to find a parameter k to characterise the output of a hall effect sensor as a function on input voltage and distance between the sensor and the magnet. However, I've found this function to be inversely proportional to the square of the distance.
I asked my professor about how to use MATLAB to find the unknown parameter, and he told me I could try to fit it by taking the logarithm of both sides of the equation and plotting that, seen as that would make the relationship linear and thus easier to plot.
I'd have to do this in MATLAB and I'm assuming the values I measured would have to be converted by hand before being able to perform any sort of curve fitting on them.
I was wondering if doing that was worth it, and if there is a faster way of doing this.
Thanks :)
In order to easily identify the relationship, for a set input voltage, I had to take the logarithm of the measured distance and the logarithm of the respective output voltages and plot those. Fitting a line through those points then enabled me to see that the coefficient was close enough to -2, confirming the inverse square relationship.
I then did the same for different input voltages and added everything together on the same plot.
I have a small project on Moving object detection in moving camera in which i have to use negative optical flow vector to minimize ego motion compensation. I have a video and some particular consecutive frames in which average of negative optical flow vector has to be computed. I have already calculated Optical flow between say, (k-1)th and kth frame. Also, I have calculated average of optical flow vector V=[u,v], where v is the average of horizontal optical flow and u is the average of vertical flow. Now, I have to apply inverse of optical flow vector i.e., -V to the (k-1)th frame. I'm new to matlab and i don't know much about it. Please help
I have tried this code segment to do so but the results aren't as expected
function I1=reverseOF(I,V)
R=I(:,:,1);
G=I(:,:,2);
B=I(:,:,3);
[m,n]=size(rgb2gray(I));
for i=1:m
for j=1:n
v1=[j i];
v2=-V;
v3=v1.*v2;
R(floor(1+abs(v3(1,2))),floor(1+abs(v3(1,2))))=R(i,j);
G(floor(1+abs(v3(1,2))),floor(1+abs(v3(1,2))))=G(i,j);
B(floor(1+abs(v3(1,2))),floor(1+abs(v3(1,2))))=B(i,j);
I1(floor(1+abs(v3(1,2))),floor(1+abs(v3(1,2))))=I(i,j);
end
end
I1=cat(3,R,G,B);
enter code here
I have used abs() function because otherwise some error was occuring like "attempted to access negative location; index must be a positive or logical".
Image A and Image B are the images that i have used to estimate the optical flow.enter image description here
This is the result that i am obtaining after applying the above function.
enter image description here
Unfortunately, you cant do this easily. This is a quite advanced research problem, because obtaining the inverse of a vector field on a mesh grid is not an easy problem, actually its quite hard.
Notice that your vector field (optical flow) start in a mesh grid, but it doesnt end in a mesh grid, it ends in random subpixel positions. If you just invert this field, doing -V is not enough! The result wont be the inverse!
This is a open research problem, look for example at this 2010 paper that addresses exactly this issue, and proposes a method to create "pseudoinverses".
Suppose you have that inverse, because you computed it somehow. Your code is quite bad for it, and the solutions (abs!) are showing (no offense) that you are not really understanding what you are doing. For a known vector field {Vx,Vy}, size equals to the image size (if its not, you can figure out easily how to interpolate it unsig interp2 ) the code would look something like:
newimg=zeros(size(I));
[ix,iy]=meshgrid(1:size(I,1),1:size(I,2));
newimg(:,:,1)=interp2(I(:,:,1),ix+Vx,iy+Vy); % this is your whole loop.
newimg(:,:,2)=interp2(I(:,:,3),ix+Vx,iy+Vy); % this is your whole loop.
newimg(:,:,3)=interp2(I(:,:,2),ix+Vx,iy+Vy); % this is your whole loop.
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))).