# I wrote this code and got this error (The function "FinalCost" was closed with an 'end', but at least one other function definition was not. To avoid confusion when using nested functions, it is illegal to use both conventions in the same file.)

3 views (last 30 days)
Harin Chhadva on 8 Apr 2020
Commented: Harin Chhadva on 8 Apr 2020
function Project_10_hpc()
%Date: 31 March, 2020
%Class: CMPSC 200
%Description: This program evaluates functions to find a zero usings
%loops.
%printheader prints the splash screen.
%calling getXY to gent the coordinates of the consumers
[x1, x2, x3, x4, x5, x6, y1, y2, y3, y4, y5, y6] = getXY('Enter the distance towards east from the origin for consumer 1:', 'Enter the distance towards east from the origin for consumer 2:', 'Enter the distance towards east from the origin for consumer 3:', 'Enter the distance towards east from the origin for consumer 4:', 'Enter the distance towards east from the origin for consumer 5:', 'Enter the distance towards east from the origin for consumer 6:', 'Enter the distance towards north from the origin for consumer 1:', 'Enter the distance towards north from the origin for consumer 2:', 'Enter the distance towards north from the origin for consumer 3:', 'Enter the distance towards north from the origin for consumer 4:', 'Enter the distance towards north from the origin for consumer 5:', 'Enter the distance towards north from the origin for consumer 6:');
%calling getV to get the volume of goods ordered by the consumers
[v1, v2, v3, v4, v5, v6] = getV('Enter the volume of consumption by consumer 1:', 'Enter the volume of consumption by consumer 2', 'Enter the volume of consumption by consumer 3:', 'Enter the volume of consumption by consumer 4:', 'Enter the volume of consumption by consumer 5:', 'Enter the volume of consumption by consumer 6:')
%calling finalCost to calculate the finalcost and the optimal values
%for x and y for the wharehouse
function [Cost, x, y] = FinalCost(x1, x2, x3, x4, x5, x6, y1, y2, y3, y4, y5, y6, v1, v2, v3, v4, v5, v6);
%printing the results
fprintf ('The Coordinates for the Wharehouse are (%3.0f, %3.0f)\nThe minimum cost of delivery is %10.2f dollars', x, y, Cost);
end
%prints my splash screen
fprintf ('Name: Harin Chhadva\nDate: 31 March 2020\nDescription: This program evaluates functions to find a zero using loops.\n');
fprintf ('\n');
end
function [x1, x2, x3, x4, x5, x6, y1, y2, y3, y4, y5, y6] = getXY(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12)
%prompting the user to input data for the location of the consummers
x1 = getDataRange(p1,0,100);
x2 = getDataRange(p2,0,100);
x3 = getDataRange(p3,0,100);
x4 = getDataRange(p4,0,100);
x5 = getDataRange(p5,0,100);
x6 = getDataRange(p6,0,100);
y1 = getDataRange(p7,0,75);
y2 = getDataRange(p8,0,75);
y3 = getDataRange(p9,0,75);
y4 = getDataRange(p10,0,75);
y5 = getDataRange(p11,0,75);
y6 = getDataRange(p12,0,75);
end
function [v1, v2, v3, v4, v5, v6] = getV(p1, p2, p3, p4, p5, p6)
%prompting the user to enter data for Volume of consumption
v1 = getDataRange(p1, 0, inf);
v2 = getDataRange(p2, 0, inf);
v3 = getDataRange(p3, 0, inf);
v4 = getDataRange(p4, 0, inf);
v5 = getDataRange(p5, 0, inf);
v6 = getDataRange(p6, 0, inf);
end
function x = getDataRange(p, a, b)
%this a recursive funciton that makes sure the the value is between a
%and b
x=input(p);
if((x<a)||(x>b))
warning('Input out of range. Try again.');
x=getDataRange(p,a,b);
end
end
function c = calcCost(x, y, x0, y0, v0)
%calculating the cost
dist = 0;
dist = sqrt(((x0 - x).^2) +((y0 - y).^2));
c = (0.5.*(dist).*v0);
end
function [Cost, x, y] = FinalCost(x1, x2, x3, x4, x5, x6, y1, y2, y3, y4, y5, y6, v1, v2, v3, v4, v5, v6)
%calculating the final cost
minCost = 1.0e99;
xW = -1;
yW = -1;
for xW = [0:100]
for yW = [ 0:75]
c1 = calcCost(xW, yW, x1, y1, v1);
c2 = calcCost(xW, yW, x2, y2, v2);
c3 = calcCost(xW, yW, x3, y3, v3);
c4 = calcCost(xW, yW, x4, y4, v4);
c5 = calcCost(xW, yW, x5, y5, v5);
c6 = calcCost(xW, yW, x6, y6, v6);
Cost = c1 + c2 + c3 + c4 + c5 + c6;
if Cost<minCost
minCost = Cost;
x = xW;
y = yW;
end
end
end
end

Tommy on 8 Apr 2020
When you call FinalCost here:
%calling finalCost to calculate the finalcost and the optimal values
%for x and y for the wharehouse
function [Cost, x, y] = FinalCost(x1, x2, x3, x4, x5, x6, y1, y2, y3, y4, y5, y6, v1, v2, v3, v4, v5, v6);
%calling finalCost to calculate the finalcost and the optimal values
%for x and y for the wharehouse
[Cost, x, y] = FinalCost(x1, x2, x3, x4, x5, x6, y1, y2, y3, y4, y5, y6, v1, v2, v3, v4, v5, v6);
Otherwise, MATLAB thinks you are defining the function rather than calling it.
##### 1 CommentShowHide None
Harin Chhadva on 8 Apr 2020
omg thanks! I copy pasted the function and didnt notice!!
thanks.