Please provide EXPLICIT MATLAB CODE. The function has one input which is an arra
ID: 3596467 • Letter: P
Question
Please provide EXPLICIT MATLAB CODE. The function has one input which is an array, and has two outputs. This function should call a function distance.m , which is included as an image. beneath this prompt. distance.m function provides the distance traveled by an electric car, given the cars charge.
[Print out and submit] Please include an example function call and the resulting output values from your function in your comments. Loops are very useful for doing the same calculation over and over, very quickly. Let's assume that we have a large fleet of electric cars, and we are trying to figure out how far all of them can drive. Write program that takes one input, an array of battery percentages. It should return two outputs: A sorted array of how far each car can drive, in descending order. The corresponding array of indices that maps the sorted array of distances to the original battery levels, so we can still keep track of which car is which. This function should NOT make any actual calculations mapping battery power to distance. Instead, it should call your function from Recitation 3 Problem 2 to make those calculations. In other words, functions that contain conditional statements will lose points! Calling other built in MATLAB functions is also acceptable, and functions, like sort and length are allowed, and may be useful to you.Explanation / Answer
Hi,
Here is the copyable code with comments to help you understand.
function[distanceTravelled]=distance(charge)
if (charge >= 81)
distanceTravelled = ( (charge-80)*10) +(30*8) +(30*7) +(15*5) +(5*4);
elseif (charge <81)&&(charge >=51)
distanceTravelled = ((charge-50)*8) + (30*7) +(15*5) +(5*4);
elseif (charge >=21)&&(charge<51)
distanceTravelled = ((charge -20)*7) +(15*5) +(5*4);
elseif (charge < 21)&& (charge>=6)
distanceTravelled = ((charge-5)*5)+(5*4);
else %charge <= 5
distanceTravelled = (charge*4);
end
function[x1,x2]=CarFleet(charges)%funcion to calculate distances
a = [];
for i=1:size(charges)%looping through array
a(i)=distance(charges(i))%calculating distance of each car
end
[x1,x2] = sort(a,'descend') %sorts the array and stores the result in x1 and indices in x2
[X,Y] = CarFleet([13,14,15])%you can call the functon like this
Thumbsup if this was helpful, otherwise let me know in comments
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.