Projectile motion [closed] - matlab

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am supposed to write a script that provides multiple lines of projectile motion but my code doesn't seem to give me what I need.
disp('This program will calculate the trajectory of a ball thrown at an initial speed vo \n')
v0 = input('Please enter the initial speed');
x0 = 0;
y0 = 0;
g = 9.81;%m/s^2
T = 5 : 5 : 85;
vx = v0*cosd(T);
vy = v0*sind(T);
t = (2*v0.*sind(T))/g;
y = y0 + (vy.*t) - ((g.*(t.^2))/2);
x = x0 + vx.*t;
plot(x,y)
This is how the graph should look like:

In your code, T represents the initial degree. You want to calculate x and y for different initial degrees (5:5:85). Use a for loop for T and plot x and y for different t.
disp('This program will calculate the trajectory of a ball thrown at an initial speed vo \n')
v0 = input('Please enter the initial speed');
x0 = 0;
y0 = 0;
g = 9.81;%m/s^2
for T = 5 : 5 : 85
vx = v0*cosd(T);
vy = v0*sind(T);
t = linspace(0,(2*v0.*sind(T))/g,100);
y = y0 + (vy.*t) - ((g.*(t.^2))/2);
x = x0 + vx.*t;
plot(x,y)
hold on
xlim([-inf inf])
ylim([-inf inf])
end
Output:
This program will calculate the trajectory of a ball thrown at an initial speed vo \n
Please enter the initial speed10

Related

Discretising a PDE using MATLAB

I want to solve the following PDE which describes the time evolution of a membrane
The PDE is discretised as follows
where
and x(s_i) = x_i, j=0,1, and x^j=x, x^j=y. We are ignoring the pressure term for now as well as constants like k_t etc.
We wish to find x(t) using a forward Euler method, by setting x = x + h*dx/dt, with h=1e-6.
My solution (for x(t)) is as follows (ignoring the y terms for ease of answering)
l = [359,1:358];
r = [2:359,1]; %left and right indexing vectors
l1 = [358,359,1:357]; %twice left neighbour
r1 = [3:359,1,2]; %twice right neighbour
%create initial closed contour with coordinates x and y
phi = linspace(0,2*pi,360); phi(end) = []; %since closed curve
x = (5 + 0.5*sin(20*phi)).*cos(phi);
y = (5+0.5*sin(20*phi).*cos(phi);
ds2 = (1/360)^2;
for i = 1:2e5
lengths = sqrt( (x-x(r)).^2 + (y-y(r)).^2 );
Tx=(1/10)/ds2*(x(r) -2*x +x(l) - x0*(((x(r)-x)/lengths)-((x-x(l))/lengths(l)) ) );
%tension term
Bx = 1/ds2^2*(x(r1) - 2*x(r) + x -2(x(r) -2*x + x(l)) + x -2*x(l) + x(l1) ); %bending term
x = x + 1/(1e6)*(Tx); % - Bx);
% Euler forward step
end
Currently, the code runs, but in the last line, if I decomment out the Bx term, the program fails to run. It seems as though my Bx matches what's in the discretisation, but obviously not.

how to find satistics of the fitted parameter of an equation [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
For instance: a, b, c and n are the three constants, which are required to be calculated by using data fitting method in a particular equation.
How can I calculate the statistics (mean, standard deviation, variance, skewness value and student t-test value) of the parameters as of a custom equation, for example the quadratic plateau equation?
Example:
x=[0,40,80,100,120,150,170,200],
y=[1865,2855,3608,4057,4343,4389,4415,4478]
y=a*(x+n)^2+b*(x+n)+c, x < xc(Ymax) ....(1) y=yp, x >= xc(Ymax) ....(2)
I have fitted this equation by given code:
yf = #(b,x) b(1).*(x+n).^2+b(2)*(x+n)+b(3); B0 = [0.006; 21; 1878];
[Bm,normresm] = fminsearch(#(b) norm(y - yf(b,x)), B0); a=Bm(1);
b=Bm(2); c=Bm(3); xc=(-b/(2*a))-n; p=p=a*(xc+n)^2+b*(xc+n)+c;
if (x < xc)
yfit = a.*(x+n).^2+ b*(x+n)+c;
else
yfit = p;
end
plot(x,yfit,'*')
hold on; plot(x,y); hold off
Note: I have already used the polyfit command, it was helpful and provided me the results. However, I really don’t find it suitable, as there is no option to customize the equation. Can I find these statistics by any code?
Questions 1, 2 and 4)
Good practice is to set initial values close to the final result if you have previous knowledge about the equation system:
What you have is an overdetermined system of linear equations.
y(1) = a*x(1)^2 + b*x(1) + c
y(2) = a*x(2)^2 + b*x(2) + c
y(3) = a*x(3)^2 + b*x(3) + c
…
y(n) = a*x(n)^2 + b*x(n) + c
or in general:
y = A*X, where
A = [a; b; c]
X = [x(1)^2 x(1) 1;
x(2)^2 x(2) 1;
x(3)^2 x(2) 1;
...
x(n)^2 x(n) 1]
One of the common practices to fit the overdetermined system (since it has no solution) is "least square fit" (mldivide,\ (link))
x=[0; 40; 80; 100; 120; 150; 170; 200];
y=[1865; 2855; 3608; 4057; 4343; 4389; 4415; 4478];
X = [x.^2 x ones(numel(x),1)];
A = y\X;
a0=A(1); %- initial value for a
b0=A(2); %- initial value for b
c0=A(3); %- initial value for c
You can customize equation, when you customize your X and A
but you also can set initial values to ones, it should have neglectable small impact on the result. More related to Question 4
a0=1;
b0=1;
c0=1;
or to random values
rng(10);
A = rand(3,1);
a0=A(1);
b0=A(2);
c0=A(3);
Question 3 - Statistics
If you need more control on monitoring of optimization process, use more general form of writing anonymous function (in code below> myfun) to save all intermediate values of parameters (a_iter, b_iter, c_iter)
function Fiting_ex()
global a_iter b_iter c_iter
a_iter = 0;
b_iter = 0;
c_iter = 0;
x=[0; 40; 80; 100; 120; 150; 170; 200];
y=[1865; 2855; 3608; 4057; 4343; 4389; 4415; 4478];
X = [x.^2 x ones(numel(x),1)];
A = y\X;
a0=A(1);
b0=A(2);
c0=A(3);
B0 = [a0; b0; c0];
[Bm,normresm] = fminsearch(#(b) myfun(b,x,y),B0);
a=Bm(1);
b=Bm(2);
c=Bm(3);
xc=-b/(2*a);
p=c-(b^2/(4*a));
yfit = zeros(numel(x),1);
for i=1:numel(x)
if (x(i) < xc)
yfit(i) = a.*x(i).^2+ b*x(i)+c;
else
yfit(i) = p;
end
end
plot(x,yfit,'*')
hold on;
plot(x,y);
hold off
% Statistic on optimization process
a_mean = mean(a_iter(2:end)); % mean value
a_var = var(a_iter(2:end)); % variance
a_std = std(a_iter(2:end)); % standard deviation
function f = myfun(Bm, x, y)
global a_iter b_iter c_iter
a_iter = [a_iter Bm(1)];
b_iter = [b_iter Bm(2)];
c_iter = [c_iter Bm(3)];
yf = Bm(1)*(x).^2+Bm(2)*(x)+Bm(3);
a=Bm(1);
b=Bm(2);
c=Bm(3);
xc=-b/(2*a);
p=c-(b^2/(4*a));
yfit = zeros(numel(x),1);
for i=1:numel(x)
if (x(i) < xc)
yfit(i) = a.*x(i).^2+ b*x(i)+c;
else
yfit(i) = p;
end
end
f = norm(y - yfit);

find nearest point to a fitting line and the neighborhood of it in Matlab

Supposed I have two random double array, which means that one x coordinate might have multiple y value.
X = randi([-10 10],1,1000);
Y = randi([-10 10],1,1000);
Then I give a line equation: y=ax+b.
I want to find the nearest point to the nearest point to the line based on every x point. And when I find this point, I will find it's neighborhood points within specific range. Please forgive my poor English, maybe following picture can help more.
Because I have a lot of data points, I hope there is an efficient way to deal with this problem.
if your X's are discrete you can try something like:
xrng = [-10 10];
yrng = [-10 10];
a = rand;
b = rand;
f = #(x) a*x + b;
X = randi(xrng,1,1000);
Y = randi(yrng,1,1000);
ezplot(f,xrng);
hold on;
plot(X,Y,'.');
xx = xrng(1):xrng(2);
nbrSz = 2;
nx = diff(xrng);
nearestIdx = zeros(nx,1);
nbrIdxs = cell(nx,1);
for ii = 1:nx
x = xx(ii);
y = f(x);
idx = find(X == x);
[~,idxidx] = min(abs(y - Y(idx)));
nearestIdx(ii) = idx(idxidx);
nbrIdxIdxs = abs(Y(nearestIdx(ii)) - Y(idx)) <= nbrSz;
nbrIdxs{ii} = idx(nbrIdxIdxs);
plot(X(nearestIdx(ii)),Y(nearestIdx(ii)),'og');
plot(X(nearestIdx(ii)) + [0 0],Y(nearestIdx(ii)) + [-nbrSz nbrSz],'g')
plot(X(nbrIdxs{ii}),Y(nbrIdxs{ii}),'sy')
end

solving a simple advection equation (1D)

I am trying to solve a 1D advection equation in Matlab as described in this paper, equations (55)-(57). I am making use of the central difference in equaton (59).
I would ultimately like to get something like figure (2) in the paper, which is the result of solving the advection equation for an advection velocity e(1-k) with k=1, i.e. a stationary wave. However, my code keeps diverging. Here is what I have so far:
%initial parameters
e = 1.0;
k = 0.5;
N = 120;
lx = 120;
%initialization of sine
for x=1:lx
if(x<3*lx/4+1 && x>lx/4+1)
phi(x) = sin(2*pi*(x-1-lx/4)/lx);
else
phi(x) = 0.0;
end
end
%advection loop
for t=1:N
gradPhi = 0.5*(+circshift(phi, [0,-1]) - circshift(phi, [0,+1]));
phiBar = phi + 0.5*k*e*gradPhi;
phiOutbar = circshift(phiBar, [0,-1]);
gradPhi = 0.5*(+circshift(phiOutbar, [0,-1]) - circshift(phiOutbar, [0,+1]));
phi = phiOutbar + 0.5*k*e*gradPhi;
end
plot(phi)
Where is the error in my simple code?
You haven't included the time step dt in your update equation. The term in equation (55) says k * e * dt / 2.
This is making your update really unstable and leading to divergence. For stability, you need CFL of 1, and yours is currently around 120. Try updating your code this way:
dt = 1/120;
%advection loop
for t=1:N/dt
gradPhi = 0.5*(+circshift(phi, [0,-1]) - circshift(phi, [0,+1]));
phiBar = phi + 0.5*dt*k*e*gradPhi;
phiOutbar = circshift(phiBar, [0,-1]);
gradPhi = 0.5*(+circshift(phiOutbar, [0,-1]) - circshift(phiOutbar, [0,+1]));
phi = phiOutbar + 0.5*dt*k*e*gradPhi;
end

How to implement an guess correcting algorithm when solving a BVP with shooting method?

I have a boundary value problem (specified in the picture below) that is supposed to be solved with shooting method. Note that I am working with MATLAB when doing this question. I'm pretty sure that I have rewritten the differential equation from a 2nd order differential equation to a system of 1st order differential equations and also approximated the missed value for the derivative of this differential equation when x=0 using the secant method correctly, but you could verify this so you'll be sure.
I have done solving this BVP with shooting method and my codes currently for this problem is as follows:
clear, clf;
global I;
I = 0.1; %Strength of the electricity on the wire
L = 0.400; %The length of the wire
xStart = 0; %Start point
xSlut = L/2; %End point
yStart = 10; %Function value when x=0
err = 5e-10; %Error tolerance in calculations
g1 = 128; %First guess on y'(x) when x=0
g2 = 89; %Second guess on y'(x) when x=0
state = 0;
X = [];
Y = [];
[X,Y] = ode45(#calcWithSec,[xStart xSlut],[yStart g1]');
F1 = Y(end,2);
iter = 0;
h = 1;
currentY = Y;
while abs(h)>err && iter<100
[X,Y] = ode45(#calcWithSec,[xStart xSlut],[yStart g2]');
currentY = Y;
F2 = Y(end,2);
Fp = (g2-g1)/(F2-F1);
h = -F2*Fp;
g1 = g2;
g2 = g2 + h;
F1 = F2;
iter = iter + 1;
end
if iter == 100
disp('No convergence')
else
plot(X,Y(:,1))
end
calcWithSec:
function fp = calcWithSec(x,y)
alpha = 0.01; %Constant
beta = 10^13; %Constant
global I;
fp = [y(2) alpha*(y(1)^4)-beta*(I^2)*10^(-8)*(1+y(1)/32.5)]';
end
My problem with this program is that for different given I's in the differential equation, I get strange curves that does not make any sense in physical meaning. For instance, the only "good" graph I get is when I=0.1. The graph to such differential equations is as follows:
But when I set I=0.2, then I get a graph that looks like this:
Again, in physical meaning and according to the given assignment, this should not happen since it gets hotter you closer you get to the middle of the mentioned wire. I want be able to calculate all I between 0.1 and 20, where I is the strength of the electricity.
I have a theory that it has something to do with my guessing values and therefore, my question is about if there is possible to implement an algorithm that forces the program to adjust the guessing values so I can get a graph that is "correct" in physical meaning? Or is it impossible to achieve this? If so, then explain why.
I have struggled with this assignment many days in row now, so all help I can get with this assignment is worth gold to me now.
Thank you all in advance for helping me out of this!