matlab - questions about slope (derivattives) - matlab

I have a function y=0.05*x.^2 - 0.24*x+(1/(x.^2+1)).
1) I want to find the slope for x [-4,4] , so I do
syms x;
y=0.05*x.^2 - 0.24*x+(1/(x.^2+1))
der=diff(y)
matrix=subs(der,x,-4:4)
and I am finding the values of y'(x) for the different values of x.
(the result is : -0.6123 -0.4800 -0.2800 0.1600 -0.2400 -0.6400 -0.2000 0 0.1323)
Now, I want to determine all the peaks and valleys of the slope.
To find this , I take from the results that for x=3 i have y'(3)=0 => I have a critical point.
So, to find the peaks and valleys I need to see the sign left and right from point 3,right?
So, for x=-4,-2 =>valley , x=-2,-1 peak, x=-1,0 valley, x=0,2 valley , x=2,4 peak.
Is this right? Also,for plotting the slope I use ezplot(der) ?
2) I need to find the drop of the slope (difference between largest ans smallest value of y).
How can I find that, since y is symbolic?
3) If I want to find the slope in degrees, how can I do it?
4) If I have x and t data (position and time) and I want to compute the velocity, I just do?
v=x./t;
result=diff(v)
--------UPDATE---------------
For my last question i have:
time=linspace(0,1.2,13);
position=[41,52,61,69,73,75,74,66,60,55,43,27,27];
v=position./time;
vel=diff(v)
plot(time,vel)
But the problem is that vel vector results in 1x12 vector instead 1x13.Why is that?

I am not really familiar with matlab, but I am going to give you some pointers with respect to the math. You define:
y(x) = 0.025*x^2 - 0.24*x + (1/(x^2+1))
This is the blue curve in the added picture. We can take the derivative with respect to x to find:
dy(x)/dx = 0.1*x - 0.24 - (2*x/(1+x^2)^2)
which is the purple curve. I do not really know what you mean with 'peaks' and 'valleys' but if you mean maxima and minima of y(x) respectively than your answer is incorrect. Maxima or minima in y(x) can be found by finding the values of x where the derivative dy/dx is zero. You can confirm this by looking at the picture. At x=3 red curve is zero because y(x) has a minimum there. (Note that by finding a point x where the derivative is zero, does not tell you whether it is in fact a maximum or a minimum, just that it is an extremum).
2) You can find the drop in the curve as follows. First determine the values of x of the maximum and the minimum x1 and x2 (i.e. solve dy(x)/dx == 0). The drop is then abs( y(x1) - y(x2) ).
3) Officially the curve does not have one slope - it is curved so its slope varies with x. However if you mean the average slope between the max and min than it is simple geometry. You have the displacement in x and y, look into the function tan and you will be able to find the answer.
Good luck

Related

Solving integral in Matlab containing unknown variables?

The task is to create a cone hat in Matlab by creating a developable surface with numerical methods. There are 3 parts of which I have done 2. My question is regarding part 3 where I need to calculate the least rectangular paper surface that can contain the hat. And I need to calculate the material waste of the paper.
YOU CAN MAYBE SKIP THE LONG BACKGROUND AND GO TO LAST PARAGRAPH
BACKGROUND:
The cone hat can be created with a skewed cone with its tip located at (a; 0; b) and with a circle-formed base.
x = Rcos u,
y = Rsin u
z = 0
0<_ u >_2pi
with
known values for R, a and b
epsilon and eta ('n') is the curves x- and y- values when the parameter u goes from 0 to 2pi and alpha is the angle of inclination for the curve at point (epsilon, eta). Starting values at A:
u=0, alhpa=0, epsilon=0, eta=0.
Curve stops at B where the parameter u has reached 2pi.
1.
I plotted the curve by using Runge-Kutta-4 and showed that the tip is located at P = (0, sqrt(b^2 + (R-alpha)^2))
2.
I showed that by using smaller intervals in RK4 I still get quite good accuracy but the problem then is that the curve isn't smooth. Therefore I used Hermite-Interpolation of epsilon and eta as functions of u in every interval to get a better curve.
3.
Ok so now I need to calculate the least rectangular paper surface that can contain the hat and the size of the material waste of the paper. If the end angle alpha(2pi) in the template is pi or pi/2 the material waste will be less. I now get values for R & alpha (R=7.8 and alpha=5.5) and my task is to calculate which height, b the cone hat is going to get with the construction-criteria alpha(2pi)=pi (and then for alpha(2pi)=pi/2 for another sized hat).
So I took the first equation above (the expression containing b) and rewrote it like an integral:
TO THE QUESTION
What I understand is that I need to solve this integral in matlab and then choose b so that alpha(2pi)-pi=0 (using the given criteria above).
The values for R and alpha is given and t is defined as an interval earlier (in part 1 where I did the RK4). So when the integral is solved I get f(b) = 0 which I should be able to solve with for example the secant method? But I'm not able to solve the integral with the matlab function 'integral'.. cause I don't have the value of b of course, that is what I am looking for. So how am I going to go about this? Is there a function in matlab which can be used?
You can use the differential equation for alpha and test different values for b until the condition alpha(2pi)=pi is met. For example:
b0=1 %initial seed
b=fsolve(#find_b,b0) %use the function fsolve or any of your choice
The function to be solved is:
function[obj]=find_b(b)
alpha0=0 %initual valur for alpha at u=0
uspan=[0 2*pi] %range for u
%Use the internal ode solver or any of your choice
[u,alpha] = ode45(#(u,alpha) integrate_alpha(u,alpha,b), uspan, alpha0);
alpha_final=alpha(end) %Get the last value for alpha (at u=2*pi)
obj=alpha_final-pi %Function to be solved
end
And the integration can be done like this:
function[dalpha]=integrate_alpha(u,alpha,b)
a=1; %you can use the right value here
R=1; %you can use the right value here
dalpha=(R-a*cos(u))/sqrt(b^2+(R-a*cos(u))^2);
end

How to order one dimensional matrices base on values

I want to determine a point in space by geometry and I have math computations that gives me several theta values. After evaluating the theta values, I could get N 1 x 3 dimension matrix where N is the number of theta evaluated. Since I have my targeted point, I only need to decide which of the matrices is closest to the target with adequate focus on the three coordinates (x,y,z).
Take a view of the analysis in the figure below:
Fig 1: Determining Closest Point with all points having minimal error
It can easily be seen that the third matrix is closest using sum(abs(Matrix[x,y,z])). However, if the method is applied on another figure given below, obviously, the result is wrong.
Fig 2: One Point has closest values with 2-axes of the reference point
Looking at point B, it is closer to the reference point on y-,z- axes but just that it strayed greatly on x-axis.
So how can I evaluate the matrices and select the closest one to point of reference and adequate emphasis will be on error differences in all coordinates (x,y,z)?
If your results is in terms of (x,y,z), why don't evaluate the euclidean distance of each matrix you have obtained from the reference point?
Sort of matlab code:
Ref_point = [48.98, 20.56, -1.44];
Curr_point = [x,y,z];
Xd = (x-Ref_point(1))^2 ;
Yd = (y-Ref_point(2))^2 ;
Zd = (z-Ref_point(3))^2 ;
distance = sqrt(Xd + Yd + Zd);
%find the minimum distance

Understanding L2-norm in MATLAB

I have two matrices, x and y, which are the same size. They are two estimations of the same data field. I want to quantify the difference between them across the whole matrix. Is norm(x-y,2) how this is normally done? What units is this in - if x and y are velocities in mm/sec, and I want to turn the L2-norm into a percentage of some reference velocity, does that make any sense?
Should this belong in Math?
Norm 2 of a matrix in Matlab is equal to root square of sum of squares of all elements. The all norm functions do not change units( its because you apply both square and root-square).
If you want compare the result with a reference velocity, it is better to use other measures like RMS (Root Mean Square). It is similar to norm but you should normalize the sum of squares before applying the root square. (this measure also does not change units)
The RMS of this matrix can be interpreted as :
How much velocity is changed at each place (x and y) in average.(the unit is mm/sec)
I'm not sure what you mean by "quantify the difference", so this is what I do know...
norm(x) == norm(x, 2)
Equivalent since L2 norm is default. From matlab help
n = norm(X) returns the 2-norm or maximum singular value of matrix X.
So, if the max singular value of the difference of your two matrices is what you want, then you have the right function.
norm(X,2) or just norm(X) will give you the l2 norm or the euclidian norm of X. X can be a matrix or a Vector. Be it a vector or a matrix, norm will be calculated by first squaring all elements, then summing them up and taking a square root yielding a single value as the answer. What you are trying to do will in some sense give you the magnitude of the difference between the two matrices. The units will be same as that of matrix elements.

How to find a position of a step that changes it's properties as function of position

I want to find a position of a step in my noisy data. After a few attempts I tried using an edge detector or to convolve with a match filter to get the position of the step, The problem that neither is accurate because the step changes its shape as function of position.
For example, given my data vector is 1000 elements long, the step width will be 30 around pixel number 200, and 70 around pixel number 700, similar to the way a wavepacket broaden due to dispersion. There are more properties that change somewhat, so the general question is, how can I find the position of the step? A matched filter is only limited to a specific shape, and will give inaccurate position. An edge detector is sensitive to the slope and also will give an inaccurate position. What other approaches do you know? I'd be happy to learn new ideas.
Here is an example of steps that change vs positions without noise or other features
And here are the same steps + noise and one additional feature (displaced for better visualization).
I would do a noise-insensitive kind of numerical differentiation, and find its peaks (I don't have Matlab at hand, so this may have typos):
n = length(x); % x is the original data vector
m = 30; % smallest step width you expect
y = zeros(n - m, 1); % the "numerical derivative" of x
for i = 1 + m : n
y(i - m) = x(i) - x(i - m);
end
figure; plot(y)
% now find the peaks of y using a sliding window and thresholding
% sliding window width should be the largest step width you expect
This simple approach worked for me in the past.
Another method to calculate numerical derivative is to calculate the slope at the middle of a (parabolic) Savitzky-Golay filter. You should apply the filter in a sliding window. Again, its width should be the smallest step width you expect. The advantages of the SG filter are that (1) calculating the slope of the fitted parabola is easy, and (2) there will be no time offset in the derivative. Calculating slope after applying a usual linear smoothing filter won't work because these filters shift the smoothed signal in time, so the timing of the step will also be shifted.

Issue in implementing mutual information

Mutual Information is defined by the formula
I(X;Y) = H(X) - H(X|Y) = H(Y) - H(Y|X)
where X,Y are some column vectors.In my case,X is a continuous signal and Y is the discretized signal. size(X)=100 and number of discretizations for Y is say 10 and word length of Y is 5(say).Now,I know that first we have to find the joint probability,then the conditional probability and then I(X,Y).In this light,I have the following implementation issues
While calculating the joint probabilities,would they be calculated till the size(x) or till word length of the discrretized signal Y?
How to obtain a single numeric value of I and a plot of I
How to find the channel capacity
Look at the last part of your equation: what is H(Y|X)? It is the entropy of Y assuming we know X... but Y is just a discretization of X! More specifically, if we know X, then we precisely know Y, and so the entropy of Y given X is zero. This leaves you with
I(X;Y) = H(Y)
Do any of your questions still need to be answered with this in mind?