MATLAB Answers

0

Sub function error within main function

Asked by Ryan Blake on 10 Nov 2019 at 23:58
Latest activity Answered by Walter Roberson
on 11 Nov 2019 at 0:31
function volSAtsa(l , w, h)
vol = volume (l, w, h);
area = sides(l, w , h);
tsa = total(a1 , a2 , a3);
output = out(vol , a1 , a2, a3 , tsa);
end
function vi = volume(l , w, h)
vi = l*w*h;
end
function a = sides(l , w , h)
a1 = l*w;
a2 = w*h;
a3 = l*h;
end
function tsarea = total(a1 , a2 , a3)
tsarea= a1 +a2 + a3;
end
function results = out(a1 , a2 , a3, tsa)
fprintf(' Volume = %.2f\n ', vi)
fprintf('Area 1 = %.2f\n ', area1)
fprintf('Area 2 = %.2f\n ', area2)
fprintf('Area 3 = %.2f\n ',area3)
fprintf('Total Area = %.2f\n ' , tsarea)
end
My program keeps giving me an error at area = sides (l,w,h). I've tried many many things to by pass it but I am running in circles at this point.

  0 Comments

Sign in to comment.

3 Answers

Answer by Shubham Gupta on 11 Nov 2019 at 0:15

Try:
function volSAtsa(l , w, h)
vol = volume (l, w, h);
[a1, a2, a3] = sides(l, w , h);
tsa = total(a1 , a2 , a3);
out(vol , a1 , a2, a3 , tsa);
end
function vi = volume(l , w, h)
vi = l*w*h;
end
function [a1,a2,a3] = sides(l , w , h)
a1 = l*w;
a2 = w*h;
a3 = l*h;
end
function tsarea = total(a1 , a2 , a3)
tsarea= a1 +a2 + a3;
end
function out(vi,a1 , a2 , a3, tsa)
fprintf(' Volume = %.2f\n ', vi)
fprintf('Area 1 = %.2f\n ', a1)
fprintf('Area 2 = %.2f\n ', a2)
fprintf('Area 3 = %.2f\n ',a3)
fprintf('Total Area = %.2f\n ' , tsa)
end

  0 Comments

Sign in to comment.


Answer by Steven Lord
on 11 Nov 2019 at 0:19

In general, when you're asking for help with an error you should show the full and exact text of the error message you receive. [Show all the text displayed in red.] That information may be very useful.
But in this case, I can see at least one problem. What does your sides function return? Where do you define a value for that variable inside your sides function?
In addition, in your call to the total function on the line after your call to the sides function you're referencing variables that don't exist in you volSAtsa function. Remember that variables defined inside a function are not generally available outside that function.

  0 Comments

Sign in to comment.


Answer by Walter Roberson
on 11 Nov 2019 at 0:31

function volSAtsa(l , w, h)
a1 = []; a2 = []; a3 = [];
vol = volume (l, w, h);
area = sides(l, w , h);
tsa = total(a1 , a2 , a3);
output = out(vol , a1 , a2, a3 , tsa);
function vi = volume(l , w, h)
vi = l*w*h;
end
function a = sides(l , w , h)
a1 = l*w;
a2 = w*h;
a3 = l*h;
a = [];
end
function tsarea = total(a1 , a2 , a3)
tsarea = a1 + a2 + a3;
end
function results = out(vol, a1 , a2 , a3, tsa)
vi = vol; area1 = a1; area2 = a2; area3 = a3; tsarea = tsa;
fprintf(' Volume = %.2f\n ', vi)
fprintf('Area 1 = %.2f\n ', area1)
fprintf('Area 2 = %.2f\n ', area2)
fprintf('Area 3 = %.2f\n ',area3)
fprintf('Total Area = %.2f\n ' , tsarea)
end
end
This seems needlessly complex to me, but you must have had some reason for writing to a1, a2, a3 without returning them. Perhaps the assignment required you to use nested functions and shared variables ???

  0 Comments

Sign in to comment.