Question

In: Computer Science

x = 1:0.5:10 and a = 7.5, what does the following code do? (x > a)...

x = 1:0.5:10 and a = 7.5, what does the following code do?
(x > a) .* (x-5).^2
Explain it qualitatively and then provide the solution.

Solutions

Expert Solution

Initially the value of a is 7.5

Initially x value is 1 and it is increment 0.5 every time upto 10.

(x > a) .* (x-5).^2

Here if x value greater than a then the value is 1(x>a condition true x>a is 1) otherwise 0

x = 1 then (1 > 7.5) .* (1-5).^2 = 0 * (-4)2 = 0 (since 1 not greater than 7.5 so it is 0)

x = 1.5 then (1.5 > 7.5) .* (1.5-5).^2 = 0 * (-3.5)2 = 0

...........................

x = 7.5 then (7.5 > 7.5) .* (7.5-5).^2 = 0 * (2)2 = 0

x = 8 then (8 > 7.5) .* (8-5).^2 = 1 * (3)2 = 9  (since 8 greater than 7.5 so it is 0)

x = 8.5 then (8.5 > 7.5) .* (8.5-5).^2 = 1 * (3.5)2 = 12.25

x = 9 then (9 > 7.5) .* (9-5).^2 = 1 * (4)2 = 16

x = 9.5 then (9.5 > 7.5) .* (9.5-5).^2 = 1 * (4.5)2 = 20.25

x = 10 then (10 > 7.5) .* (10-5).^2 = 1 * (5)2 = 25

Program:

a = 7.5; % Initially the value of a is 7.5
for x = 1:0.5:10 % Loop runs from 1 to 10
fprintf("The value @x=%.2f is %.2f\n",x,(x > a) .* (x-5).^2); % print x and the result and (x > a) .* (x-5).^2
end

Output:


Related Solutions

Suppose x is 1 and y is 10. What is the output of the following code?...
Suppose x is 1 and y is 10. What is the output of the following code?                         If (x> 1) && ( y <11)                                     Display statement                         else                                     Display nothing 2. Suppose x is 1 and y is 10. What is the output of the following code?                         If (x> 1) || ( y <11)                                     Display statement                         Else                                     Display nothing 3. Write a java program that prints 1 to infinite numbers. 4. The...
A cylindrical container with do = 0.5 m and L = 7.5 m is used for...
A cylindrical container with do = 0.5 m and L = 7.5 m is used for chemical processing with insulated ends. The inside of the cylinder is kept at 400o C with 5 kW of power coming from a heater. The outer surface temperature is known to be 78 oC and ambient air in the factory is 20C. a.) Estimate the heat transfer coefficient between the surface of the cylinder and the ambient air and find the thermal resistance between...
I'm having trouble understanding the following code (a snippet of a code). What does it do?...
I'm having trouble understanding the following code (a snippet of a code). What does it do? The whole code is about comparing efficiencies of different algorithms. def partition(list,first,last): piv = list[first] lmark = first+1 rmark = last done = False while not done: while lmark <= rmark and list[lmark]<=piv: lmark=lmark+1 while list[rmark]>=piv and rmark>=lmark: rmark=rmark-1 if rmark<lmark: done = True else: temp = list[lmark] list[lmark]=list[rmark] list[rmark]=temp temp = list[first] list[first]=list[rmark] list[rmark]=temp return rmark
Explain the following code. What language? What does it do? What is the result?                 <!DOCTYPE...
Explain the following code. What language? What does it do? What is the result?                 <!DOCTYPE teo[                                 <ELEMENT teo((stations| databases)+)>                                 <ELEMENT stations(stationName stationLocation sensors*)> <ELEMENT databases(databaseName databaseType )> <ELEMENT stationName(#PCDATA)> <ELEMENT stationLocation(#PCDATA)> <ELEMENT sensors (sensorName phenomenon)+> <ELEMENT sensorName(#PCDATA)> <ELEMENT phenomenon(#PCDATA)> <ELEMENT databaseName(#PCDATA)> <ELEMENT databaseType(#PCDATA)> <!ATTLIST stations boundingBox CDATA #required> ]>
What is the output of the following code: x = 0 a = 1 b =...
What is the output of the following code: x = 0 a = 1 b = -3 if a > 0:    if b < 0:       x = x + 5    elif a > 5:       x = x + 4    else:       x = x + 3 else:     x = x + 2 print(x) 4 2 5 3 Consider the following code segment:    sum = 0.0    while True:       number = input(“Enter a...
Ka for arsenic acid, HAsO4^2-, is 7.5 x 10^-12. What is the pH of a 0.15...
Ka for arsenic acid, HAsO4^2-, is 7.5 x 10^-12. What is the pH of a 0.15 Maqeous soltuion of AsO4^3-?
The molar solubility of BaF2 is 7.5 x 10-3 mol/L. What is the value of Ksp...
The molar solubility of BaF2 is 7.5 x 10-3 mol/L. What is the value of Ksp for BaF2?
Study the following statements. Evaluate whether these statements are TRUE or FALSE [10 X 0.5 =...
Study the following statements. Evaluate whether these statements are TRUE or FALSE [10 X 0.5 = 5 points] In liberal economic theories, political power is equated with wealth. The idea of “embedded liberalism” advocates a compromise between meeting domestic economic objectives and openness to international trade and investment. A key tenet of economic nationalism is that the economic well-being of individuals is subordinate to the goal of state-building and the interests of the state. Inclusive club goods are referred to...
a random variable X has the following pmf: X -1 0 1 P[X] 0.25 0.5 0.25...
a random variable X has the following pmf: X -1 0 1 P[X] 0.25 0.5 0.25 Define Y = X2 & W= Y+2. Which one of the following statements is not true? V[Y] = 0.25. E[XY] = 0. E[X3] = 0. E[X+2] = 2. E[Y+2] = 2.5. E[W+2] = 4.5. V[X+2] = 0.5. V[W+2] = 0.25. P[W=1] = 0.5 X and W are not independent.
. What does each line of the following code do? fsrPin = ‘A4’; ledPin = ‘D4’;...
. What does each line of the following code do? fsrPin = ‘A4’; ledPin = ‘D4’; anArduinoObj = arduino(); forceVoltage = readVoltage(anArduinoObj, fsrPin); if forceVoltage > 4.0 writeDigitalPin(anArduinoObj, ledPin, 1); end Based on the Adafruit pages related to their round FSR sensor, what is the required force to read a voltage greater than 4V?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT