Simulink differential equation, sampling time? - simulink

I have a circular movement which I want to simulate. I start with the equation phi, and then I want to transform the result into x and y coordinates for the next differential equation.
But here comes the problem: when I transform the signal it becomes a zig-zag-pattern not a normal sinus curve.
appendix:
circular movement
differential eq
"transform phi to x signa"
result
Differential eq (Übersetzter Weg means transformer):
Image: circular movement
Image: transformation
Image: wave

Related

Struggling with plotting the solution of ODE having step function using MATLAB

I am trying to use MATLAB to solve the ODE involving step function
Here is my code to solve the above ODE:
syms t s Y y(t) Dy(t) u(t) f(t) k
Dy = diff(y,t)
D2y = diff(Dy,t)
f = heaviside(t) + 2*symsum((-1)^k*heaviside(t-k*pi()),k,1,Inf)
eqn = D2y +0.1*Dy + y == f
leqn = laplace(eqn,t,s)
LT_Y=subs (leqn, laplace (y,t,s),Y);
LT_Y=subs (LT_Y, y(0), 0);
LT_Y=subs(LT_Y, subs(diff(y(t), t), t, 0), 0);
Y=solve(LT_Y,Y);
y=ilaplace(Y,s,t)
However, the result seems strange and I cannot plot the graph of the solution. Could anyone tell me what should I do to my code so that I can plot the solution? Thank you so much
The decay rate of the friction term is 0.1/2=0.05, so you need about t=40 for a decay of 0.1. In a standard plot features down to 1% of the plot size are visible, which would require a time span of t=80, or longer for some visual confirmation of the stabilization of the amplitude at that level.
The right side is about in resonance with the left one, so the steady-state solution will roughly look like -10*cos(t) plus higher frequency components.
Just for plotting a numerical solution is sufficient
opts=odeset("AbsTol",1e-6,"RelTol",1e-9);
[T,Y] = ode45(#(t,y)[y(2); f(t)-0.1*y(2)-y(1)], [0, 100], [0;0], opts);
plot(T,Y(:,1),T,-13*cos(T));
function z=f(t)
z = sign(sin(t));
end
This gives the plot below, blue for the solution and green for the reference (guessed amplitude)
Computing the Fourier series for the right side, the amplitude of the cosine component of the solution is 40/pi=12.732.

How to calculate the point-by-point radius of curvature of a trajectory that is not a proper function

with Matlab i'm trying to calculate the "radius of curvature" signal of a trajectory obtained using GPS data projected to the local cartesian plane.
The value of the signal onthe n-th point is the one of the osculating circle tangent to the trajectory on that point.
By convention the signal amplitude has to be negative when related to a left turn and viceversa.
With trajectories having a proper function as graph i'm building the "sign" signal evaluating the numeric difference between the y coordinate of the center of the osculating circle:
for i=1:length(yCenter) -1
aux=Y_m(closestIndex_head:closestIndex_tail );
if yCenter(i) - aux(i) > 0
sign(i)=-1;
else
sign(i)=+1;
end
end
yCenter contains x-coordinates of all osculating circles related to each point of the trajectory;
Y_m contain the y-coordinates of every point in trajectory.
The above simple method works as long as the trajectory's graph is a proper function (for every x there is only one y).
The trajectory i'm working on is like that:
and the sign signal got some anomalies:
The sign seems to change within a turn.
I've tried to correct the sign using the sin of the angle between the tangent vector and the trajectory, the sign of the tangent of the angle and other similar stuff, but still i'm looking at some anomalies:
I'm pretty sure that those anomalies came from the fact that the graph is not a proper function and that the solution lies on the angle of the tangent vector, but still something is missing.
Any advice will be really appreciated,
thank you.
Alessandro
To track a 2D curve, you should be using an expression for the curvature that is appropriate for general parametrized 2D functions.
While implementing the equation from Wikipedia, you can use discrete differences to approximate the derivatives. Given the x and y coordinates, this could be implemented as follows:
% approximate 1st derivatives of x & y with discrete differences
dx = 0.5*(x(3:end)-x(1:end-2))
dy = 0.5*(y(3:end)-y(1:end-2))
dl = sqrt(dx.^2 + dy.^2)
xp = dx./dl
yp = dy./dl
% approximate 2nd derivatives of x & y with discrete differences
xpp = (x(3:end)-2*x(2:end-1)+x(1:end-2))./(dl.^2)
ypp = (y(3:end)-2*y(2:end-1)+y(1:end-2))./(dl.^2)
% Compute the curvature
curvature = (xp.*ypp - yp.*xpp) ./ ((xp.^2 + yp.^2).^(1.5))
For demonstration purposes I've also constructed a synthetic test signal (which can be used to recreate the same conditions), but you can obviously use your own data instead:
z1 = linspace(2,1,N).*exp(1i*linspace(0.75*pi,-0.25*pi,N))
z2 = 2*exp(-1i*0.25*pi) + linspace(1,2,N)*exp(1i*linspace(0.75*pi,2.25*pi,N))
z = cat(1,z1,z2)
x = real(z)
y = imag(z)
With the corresponding curvature results:

Why there is not dxdy factor in fft2() function of MATLAB?

In analytical 2D Fourier Transform, integrand is multiplied by dxdy.
In algorithm of MATLAB function fft2(), this is not done.
In discrete case, dx is Lx/Nx and dy = Ly/Ny,
where Lx,Ly are lengths in metres and Nx,Ny are number of samples in x,y directions.
https://in.mathworks.com/help/matlab/ref/fft2.html
Is it not correct to multiply by dxdy after using fft2() for user function i.e. matrix ?
Similarly for ifft2() function in MATLAB, there is no (dk_x)*(dk_y)
where dk_x = 1/Lx and dk_y = 1/Ly
https://in.mathworks.com/help/matlab/ref/ifft2.html
Any help will be appreciated.
Thanks.

Returning original mathematical function values from ode45 in Matlab?

My intention is to plot the original mathematical function values from the differential equation of the second order below:
I(thetadbldot)+md(g-o^2asin(ot))sin(theta)=0
where thetadbldot is the second derivative of theta with respect to t and m,d,I,g,a,o are given constants. Initial conditions are theta(0)=pi/2 and thetadot(0)=0.
My issue is that my knowledge and tutoring is limited to storing the values of the derivatives and returning them, not values from the original mathematical function in the equation. Below you can see a code that calculates the differential in Cauchy-form and gives me the derivatives. Does anyone have suggestions what to do? Thanks!
function xdot = penduluma(t,x)
% The function penduluma(t,x) calculates the differential
% I(thetadbldot)+md(g-o^2asin(ot))sin(theta)=0 where thetadbldot is the second
% derivative of theta with respect to t and m,d,I,g,a,o are given constants.
% For the state-variable form, x1=theta and x2=thetadot. x is a 2x1 vector on the form
% [theta,thetadot].
m=1;d=0.2;I=0.1;g=9.81;a=0.1;o=4;
xdot = [x(2);m*d*(o^2*a*sin(o*t)-g)*sin(x(1))/I];
end
options=odeset('RelTol', 1e-6);
[t,xa]=ode45(#penduluma,[0,20],[pi/2,0],options);
% Then the desired vector from xa is plotted to t. As it looks now the desired
% values are not found in xa however.
Once you have the angle, you can calculate the angular velocity and acceleration using diff:
options=odeset('RelTol', 1e-6);
[t,xa]=ode45(#penduluma,[0,20],[pi/2,0],options);
x_ddot = zeros(size(t));
x_ddot(2:end) = diff(xa(:,2))./diff(t);
plot(t,xa,t,x_ddot)
legend('angle','angular velocity','angular acceleration')
which gives the following plot in Octave (should be the same in MATLAB):
Alternatively, you can work it out using your original differential equation:
x_ddot = -m*d*(o^2*a*sin(o*t)-g).*sin(xa(:,1))/I;
which gives a similar result:

Calculate the power in a bow string using matlab

I'm trying to calculate the force in a bow string while using MATLAB's ode45, I have this differential equation
M(x)=d^2y/dx^2 * (1+(dy/dx)^2)^(-3/2)=-q * y where M(x) is the average torque in the bow, q=11.4716, and y(0)=0.3, y'(0)=0 and 0≤ x ≤0.3668
I have calculated the differential equation with ode45 but I have tried to calculate the force with this code;
X=0.3668
tolerance=odeset('Abstol', 1e-9, 'Reltol', 1e-11);
[x,M]=diffekv45(tolerance,X)
force=zeros(length(x),1);
for i=1:length(x)
if x(i)==0
force(i)=0;
else
force(i)=(0.3-M(i,1))./x(i); %%% 0.3-M(i,1) is because it is equilibrium in the %%center of the bow
end
end
totalforce=sum(force);
Line 6 is calculating the torque in all the readings. I suppose that the first element in M is the average torque.
I'm using these functions
function [x,M] = diffekv45(tolerance,X)
u0=[0.3;0];
[x,u]=ode45('M',[0,X],u0,tolerance);
end
function M=M(~,u)
global q
M=[u(2)
-q*u(1)*(1+u(2)^2)^(3/2)];
end
When I calculate the force with a higher or lower tolerans I get a significant higher or lower value of the force. That's why I'm wrong. So how can I calculate the force in the bow string?
You have a 2nd order equation in terms of the displacement, which needs to be split up into two first order equations for use in orde45().
Use a state vector h=[y; diff(y,x)] and create the following differential function for use in ode45()
function hp = deriv(x,h)
y = h(1); %First element of h is the deflection
v = h(2); %Last element of h is the slope
hp = [v; -q*y/(1+v^2)^(-3/2)];
end
and then call
h0 = [0.3; 0];
ode45(deriv, [0,X], h0, tol);