1. (10 points A Monte Carlo analysis can be used for optimization. For example,
ID: 639966 • Letter: 1
Question
1. (10 points A Monte Carlo analysis can be used for optimization. For example, the trajectory of a ball can be computed with 2vo cos2 Go where y is the achieved height in meters (m), 6% is the initial angle (radians), is the horizontal distance (m), g is the gravitational acceleration (-9.81 m/s2), vo is the initial velocity (m/s), and yo is the initial height. Given yo 1 m, vo 25 m/s, and 500 (or T/180 x 50 rad), determine the maximum height and the corresponding z distance numerically with Monte Carlo simulation. Develop a function calc trajectory that generates a vector of 10,000 uniformly distributed values ofr between 0 and 60 m using the rand function. Use this vector and the above equation to generate a vector of heights. Determine the maximum height and the associated z distance. Your function template is as follows: 1 function [hmax xmax] calc trajectory(s) 3 rng(s); 8 Initialize the random number generator with the integer s 5 8 Rest of your code goes here Your code must return the maximum height 6 8 achieved in the variable hmax and the corresponding x distance in xmax. 8 end Note that we control the random number generator using the rng function. It seeds the random number generator using the non-negative integer s supplied to your function so that rand produces a predictable sequence of numbers each time the function is called.Explanation / Answer
Matlab Code:
format short
gg=9.81;
y0=1;
v0=25;
theta0=50*pi/180;
s=10000;
xmin=0;
xmax=60;
r=rand(s,1);
xrand=xmin+(xmax-xmin)*r;
yrand=tan(theta0)*xrand-gg/(2*v0^2*cos(theta0)^2)*xrand.^2+y0;
[ymax,i]=max(yrand);
xmax=xrand(i);
y=@(x)-tan(theta0)*x-gg/((2*v0^2*cos(theta0)^2)*x.^2+y0);
[xmax,hmax]=calc_trajectory(y,0,60)
Output:
xmax =
59.9999
hmax =
-71.5052
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.