Write a Matlab script to do the following. (a) Generate consecutive random numbe
ID: 3704090 • Letter: W
Question
Write a Matlab script to do the following. (a) Generate consecutive random numbers on the interval [0, 1] until the sum of their squares exceeds 1000 and report the number of random numbers needed to achieve this. (b) Repeat part (a) 10000 times and record the average of the numbers thus obtained. Question 4 Write a Matlab script that does the following. For this question you need to use logic operators, but you cannot use "for" loops, except possibly to print your foci (see below). (a) Create, with the Matlab function "meshgrid" a 1000 x 1000 square grid in the plane, centered at the origin, with sidelength 20. (b) In Figure 1, plot all the grid points for which the sum of the distances to four given points (foci) does not exceed 30. Plot these points in blue. The four given points areExplanation / Answer
If you post more than 1 question, as per chegg guidelines I have to solve only first question.
Ques 3.
-------------------------fxn.m------------------------
function [n] = fxn()
% store the sum of squares
sum_of_squares = 0;
n = 0;
% infinite loop
while true
% get the current term
% generate random number in range [ 0 , 1 ]
temp = rand();
% add the aquare of current term to sum_of_squares
sum_of_squares = sum_of_squares + ( temp ^ 2 );
% if the sum exceeds 1000
if sum_of_squares > 1000
break;
end
n = n + 1;
end
end
---------------------------main.m-----------------------
n = fxn();
fprintf('No of terms : %d ', n);
Sample Output
No of terms : 3055
(b)
-------------------------fxn.m------------------------
function [n] = fxn()
% store the sum of squares
sum_of_squares = 0;
n = 0;
% infinite loop
while true
% get the current term
% generate random number in range [ 0 , 1 ]
temp = rand();
% add the aquare of current term to sum_of_squares
sum_of_squares = sum_of_squares + ( temp ^ 2 );
% if the sum exceeds 1000
if sum_of_squares > 1000
break;
end
n = n + 1;
end
end
---------------------------main.m-----------------------------
count = 0;
for i = 1 : 10000
% get the number of terms
n = fxn();
count = count + n;
end
% calculate average
avg = count / 10000;
fprintf('Average no of terms : %d ', avg);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.