in matlab
(a)Write a function to generate n random integers between -b and b.
(b) Using the function you wrote in (a) generate 50 random numbers in a vector A where b = 100;
(c) This array A will contain both negative, positive and possibly zero entries. Write a MATLAB script to compute the percentage of positive, negative and zero entries in the array.
Question (a)
The MATLAB Function is written as follows
function [A] = myrandfunc(n, b)
% MYRANDFUNC generates n random numbers between-b and +b
% n = Number of random numbers to be generated
% b = This gives the range [-b, b] in which the random number is to
be generated
% A = Output of the function containing the n random numbers
range = [-b, b]; % The Range
A = randi(range, n, 1); % Generating n random numbers
end
This is saved with the same name myrandfunc
Question (b)
The function is called as follows in the command prompt
After entering this command, we will get the result as follows
>> A = myrandfunc(50, 100)
A =
94
52
82
77
-69
33
-67
2
54
-73
78
0
-35
95
51
14
64
46
-55
-58
-38
4
14
46
-80
-48
-45
39
88
-79
67
-69
32
77
95
-81
40
-74
20
-55
-42
-21
-69
55
-55
-49
57
-84
-74
-2
>>
So we can see 50 random integers between -100 and 100
Question (c)
MATLAB Code
clc;
clear all;
n = input('Enter the number of random integers to be generated:
');
b = input('Enter the value of b: ');
A = myrandfunc(n, b);
np = 0; % Initialize number of positive integers to 0
nn =0; % Initialize number of negative integers to 0
nz = 0; % Initialize number of zeros to 0
for n = 1:n
if A(n) > 0
np = np + 1;
elseif A(n) < 0
nn = nn +1;
else
nz = nz + 1;
end
end
P = np/n*100;
N = nn/n*100;
Z = nz/n*100;
fprintf('The percentage of positive integers generated is %f
\n', P);
fprintf('The percentage of negative integers generated is %f \n',
N);
fprintf('The percentage of zeros generated is %f \n', Z);
After executing, we can enter the number of integers to be generated and the range as follows
Enter the number of random integers to be generated: 50
Enter the value of b: 100
The percentage of positive integers generated is 40.000000
The percentage of negative integers generated is 58.000000
The percentage of zeros generated is 2.000000
>>
Another Code:
clc;
clear all;
n = input('Enter the number of random integers to be generated:
');
b = input('Enter the value of b: ');
A = myrandfunc(n, b);
pi = find(A>0); % Find the indices of positive integers in
A
ni = find(A<0); % Find the indices of negative integers in
A
zi = find(A==0); % Find the indices of zeros in A
np = length(pi); % Find the number of positive integers
nn = length(ni); % Find the number of negative integers
nz = length(zi); % Find the number of zeros
P = np/n*100; % Calculate the percentage of positive numbers
N = nn/n*100; % Calculate the percentage of negative numbers
Z = nz/n*100; % Calculate the percentage of zeros
fprintf('The percentage of positive integers generated is %f
\n', P);
fprintf('The percentage of negative integers generated is %f \n',
N);
fprintf('The percentage of zeros generated is %f \n', Z);
After executing, we can enter the number of integers to be generated and the range as follows
Enter the number of random integers to be generated: 50
Enter the value of b: 100
The percentage of positive integers generated is 42.000000
The percentage of negative integers generated is 56.000000
The percentage of zeros generated is 2.000000
>>
in matlab (a)Write a function to generate n random integers between -b and b. (b) Using...
In matlab script, thank you! Write a MATLAB script that will generate random integers in the range from 1 to 100, and print them, until one is finally generated that is greater than 50. The script should print how many attempts it took
Write a MATLAB code to generate an array of 100 random integers between 1 and 10 using the command “randi”. Using “if” statement, impose the condition that if a number in the array is greater than 5, the number is modified as, number = number – 11. Find the sum of the array and store it. Repeat this exercise for 10, 1000 and 100000 trials and plot the following: (a) Sums obtained vs trails number (b) Histogram of the sum...
Write a script that will create a (random) vector of ages (real numbers, not integers). The script should contain the following prompts: - the user will provide the number of elements in the vector. - the user will provide the age of the oldest person in the group. For example: Enter the number of persons in the group: 50 Enter the age of the oldest person: 37 Save the vector containing the ages in a “.dat file” called ages_vector.dat matlab...
1) a) Write MATLAB function that accepts a positive integer parameter n and returns a vector containing the values of the integral (A) for n= 1,2,3,..., n. The function must use the relation (B) and the value of y(1). Your function must preallocate the array that it returns. Use for loop when writing your code. b) Write MATLAB script that uses your function to calculate the values of the integral (A) using the recurrence relation (B), y(n) for n=1,2,... 19...
Write a user-defined MATLAB function that gives a random integer number within a range between two numbers. For the function name and arguments, use n = randint(a,b) where the two input arguments a and b are the two numbers and the output argument n is the random number. Use the function in the command window for the following: (a) Generate a random number between 1 and 49. (b) Generate a random number between -35 and -2
Consider an array of length n containing positive and negative integers in random. Write a C++ code that rearranges the integers so that the negative integers appear before the positive integers. Your solution should use: a. O(n^2) b. O(n)
Write a MATLAB script, using either a single or nested for-loop, that will print the factorials for the numbers between 1 and 100 (inclusive). The factorial of n (n!) is the product of the positive integers less than or equal to n. For example: 3! = 3 * 2 * 1. For this question you cannot use the built-in MATLAB factorial function, but you can use other MATLAB functions if you wish. MATLAB code!MATLAB code!MATLAB code!MATLAB code!
Please answer this question using Matlab: Write a matlab function to generate and plot a sine wave at a specified frequency having at least 10 cycles, for this case the amplitude will represent voltage. Your sine wave should have a positive going zero crossing at T=0. Make sure your plot has at least 25 samples per cycle. Make sure the plot is with respect to time. Your function needs to return your generated samples and time vector. The frequency will...
Write a script that prompts the user to enter a value n (n > 6) that will generate an n element vector of random integers between 0 and 100 that will represent quiz grades. Then have the script calculate the average of the quizzes where the smallest grade is dropped. Then script will display the average of the quizzes along with the highest quiz grade. Given the following system of equations: 8s - 11t + 7u + 5v - w...
MATLAB homework assignment (HW6), but this time using a "vectorized" approach. Write a script that prompts the user for N integers (set N-6), one at a time, using a while loop. Error-check to ensure that each number entered is an integer, displaying an error message if it is not. If it is an integer, store it in an array variable nums. Continue prompting until N integers have been successfully entered. (You may copy and paste your code from the previous...