Solving integral in Matlab containing unknown variables? - matlab

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

Related

Symbolic Small Quantity Approximation in MATLAB

I am trying to make a small angle approximation in MATLAB's symbolic toolbox. This is being used for the equations of motion in a spacecraft control simulation (and yes, I need to linearize, I can't leave them in their more exact form). For those unfamiliar, the small quantity approximation does a few main things that I need. For small quantities delta and gamma,
delta times gamma is approximately 0
delta^2 is approximately 0 (same with higher powers)
sin(delta) is approximately delta
cos(delta) is approximately 1
I have tried using MATLAB's taylor function (link here), but it doesn't seem to be doing what I want except in a very specific scenario (which I am sure is coincidental anyway). A test case is presented below:
syms psiX psiY psiZ rGMag mu Ixx Iyy Izz
QLB = [1,psiZ,-psiY;-psiZ,1,psiX;psiY,-psiX,1]; %linearized version of the rotation matrix from the L frame to the B frame
rG_LVLH = [0;0;rGMag]; %magnitude of the rG vector expressed in the L frame
rG = QLB*rG_LVLH
G = 3*mu/rGMag^5 .* [rG(2)*rG(3)*(Izz-Iyy);rG(1)*rG(3)*(Ixx-Izz);rG(1)*rG(2)*(Iyy-Ixx)]; %gravity-gradient torque
The desired output of the above should have the G vector with a 0 in the third component and symbolic variables left in the other two. This particular example doesn't include a trigonometric example, but I can provide if necessary. Thanks.

Computing the DFT of an arbitrary signal

As part of a course in signal processing at university, we have been asked to write an algorithm in Matlab to calculate the single sided spectrum of our signal using DFT, without using the fft() function built in to matlab. this isn't an assessed part of the course, I'm just interested in getting this "right" for myself. I am currently using the 2018b version of Matlab, should anyone find this useful.
I have built a signal of a 1 KHz and 2KHz sinusoid, phase shifted by 135 degrees (2*pi/3 rad).
then using the equations in 9.1 of Discrete time signal processing (Allan V. Oppenheim) and Euler's formula to simplify the exponent, I produce this code:
%%DFT(currently buggy)
n=0;m=0;
for m=1:DFT_N-1 %DFT_Fmin;DFT_Fmax; %scrolls through DFT m values (K in text.)
for n=1:DFT_N-1;%;(DFT_N-1);%<<redundant code? from Oppenheim eqn. 9.1 % eulers identity, K=m and n=n
X(m)=x(n)*(cos((2*pi*n*m)/DFT_N)-j*sin((2*pi*n*m)/DFT_N));
n=n+1;
end
%m=m+1; %redundant code?
end
This takes x as the input, in this case the signal mentioned earlier, as well as the resolution of the transform, as represented by the DFT_N, which has been initialized to 100. The output of this function, X, should be something in the frequency domain, but plotting X yields a circular plot slightly larger than the unit circle, and with a gap on the left hand edge.
I am struggling to see how I am supposed to convert this to the stem() plots as given by the in-built DFT algorithm.
Many thanks, J.
This is your bug:
replace X(m)=x(n)*(cos.. with X(m)=X(m)+x(n)*(cos..
For a given m, it does not integrate over the variable n, but overwrites X(m) only the last calculation for n = DFT_N-1.
Notice that integrating over n=1:DFT_N-1 omits one harmonic, i.e., the first one, exp(-j*2*pi). Replace
n=1:DFT_N-1 with n=1:DFT_N to include that. I would also replace m=1:DFT_N-1 with m=1:DFT_N for plotting concerns.
Also replace any 2*pi*n*m with 2*pi*(n-1)*(m-1) to get the phase right, since the first entry of X should correspond to zero-frequency, yielding sum_n x(n) * (cos(0) + j sin(0)) = sum_n x(n). If your signal x is real-valued then the zero-frequency component X(1) should be real-valued, angle(X(1))=0.
Last remark, don't forget to shift zero-frequency component to the center of the spectrum for better visibility, X = circshift(X,floor(size(X)/2));
If you are interested in the single-sided spectrum only, than you can just calculate X(m) for m=1:DFT_N/2 since X it is conjugate symmetric around m=DFT_N/2, i.e., X(DFT_N/2+m) = X(DFT_N/2-m)', due to exp(-j*(pi*n+2*pi/DFT_N*m)) = exp(-j*(pi*n-2*pi/DFT_N*m))'.
As a side note, for a given m this program calculates an inner product between the array x and another array of complex exponentials, i.e., exp(-j*2*pi/DFT_N*m*n), for n = 0,1,...,N-1. MATLAB syntax is very convenient for such calculations, and you can avoid this inner loop by the following command
exp(-j*2*pi/DFT_N*m*(0:DFT_N-1)) * x
where x is a column vector. Similarly, you can avoid the first loop too by expanding your complex exponential vector row-wise for every m, i.e., build the matrix exp(-j*2*pi/DFT_N*(0:DFT_N-1)'*(0:DFT_N-1)). Then your DFT is simply
X = exp(-j*2*pi/DFT_N*(0:DFT_N-1)'*(0:DFT_N-1)) * x
For single-sided spectrum, instead use
X = exp(-j*2*pi/DFT_N*(0:floor((DFT_N-1)/2))'*(0:DFT_N-1)) * x

Integration with Probability Density Function

I need to plot the probability density function {p(z,phi)}and need to integrate it,as shown in the attached eq.#1
enter image description here
where Af and Vf are constants,
phi is angle,
z is distance(numerical value, can be in decimals)
The P(z,phi) will be the force values along with respective different values of z and phi.
Could someone guide me, on MATLAB, how can I write these set of equations?
To intregate your function, you should either create an m-file or anonymous function
f = #(z,phi) P(z,phi) * p(z,phi)
where you construct P and p similarly. Then you will need to use one of the numerical integrators, such as ode45 to integrate f twice... once over f and once over phi.
If I understand you correctly you multiply a uniform probability distribution between -lf/2 and lf/2 with another probability distribution that looks like the first quarter of a sine wave. You want to know the resulting probability distribution.
Basically if lf/2 > pi/2 you end up with the same distribution. The sine-distribution is entirely inside the uniform distribution. If (lf/2)<(pi/2) the uniform-distribution chops of part of your sine-distribution. You then want to divide your probability distribution by the part you choped off so the integral stays one. It must remain a probability distribution.
The integral of sin(x) is cos(x). So in that case you devide by (1-cos(lf/2))
Below is a script that makes it more visible:
lf=2;
xx = linspace(-lf,lf,1E4);
p1 = (xx>-lf/2&xx<lf/2)*(1/lf);
p2 = zeros(size(xx));
p2(xx>0&xx<pi/2) = sin(xx(xx>0&xx<pi/2));
p3 = p2.*p1.*lf;
if lf<pi
p3 = p3./(1-cos(lf/2));
end
plot(xx,p1,xx,p2,xx,p3)
legend({'uniform distribution','sine','result'})
%integrals (actually Riemann sums):
sum(p1.*(xx(2)-xx(1)))
sum(p2.*(xx(2)-xx(1)))
sum(p3.*(xx(2)-xx(1)))

Using diff or gradient for symbolic computations?

I am using virtual potential fields to control the movement of a group of robots in a 2D environment, their position is given by a matrix of x and y coordinates. The virtual potential fields depend on a number of variables, one of them is the inter-robot distance. A short (heavily simplified) example of my code is given below.
x = sym('x',[4 2]); % four robots with x and y coordinates
xd = sym('xd',[1 2]); % a single destination
F = sym(ones(4,1)); % one potential function for each robot
for i=1:size(x,1)
for j=1:size(x,1)
if i~=j
F(i) = F(i)/norm(x(i,:)-x(j,:))^2; % infinite potential when any two robots collide
end
end
F(i) = F(i) * norm(x(i,:)-xd)^2; % add an attraction force to the goal
end
So now that I have created symbolic expressions for the potential fields, I need to find their derivative so I can apply steepest descent. Now I'm wondering: does it make any difference whether I use the function gradient or diff to obtain the derivative with respect to the position? To clarify: for robot i I want to take the derivative with respect to xi_1 and xi_2.
Your question, as stated, is bordering on mathematics rather than programming. The gradient is just the generalization of the derivative to multiple dimensions. Yes, for movement in a 2-D plane, it would make sense to use sym/gradient. As the documentation states, if you specify just a scalar for the second argument, sym/gradient becomes equivalent to sym/diff. To properly calculate your 2-D gradient, the second argument must be a two-element vector, e.g., [xi_1 xi_2].

drawing a contour for n* 1 matrix

I am new to matlab and simulink.
I have a function P which take two inputs distance and velocity (each one as a function)..
I modeled my system in simulink and I wrote the P function in the block called Matlab function since the function has if statement for example ( if v < 0 then P = 1 else P = 1 / v+d ).
The velocity and the distance function are in form of sine and cosine with different frequencies..
Now I added the block to workspace for P, velocity and distance as an array. In the workspace I have array of n*1 for all of the three.
I want to draw a contour for the P according to the velocity and the distance but the contour accept at least a matrix 2*2 ..
How can I change the P to be a matrix without affecting my work and I want to draw the contour for P with respect to the velocity and the distance ??
Here is what you can do:
See which speeds and velocities you have, and define a 2 dimensional grid based on that.
Try to give each point on the grid the correct value (based on your simulation output)
Use the contour function.
Note that drawing a contour mostly makes sense if you have a significant part of the grid covered. Otherwise try help plot3 as #thewaywewalk suggested.