no input, two output function

The first part of this assignment is to create a function with no inputs and two outputs. The 15x1 matrices are X and Y so I've got
function [X,Y] = myfunct()
X=[1;2;3 etc]
Y=[1;2;3 etc]
plot(X,Y,'.b')
end
If I run the function, the only thing that appears in the workspace is ans. Is this because they are local variables? How do I assign them to memory? Because the next part of the assignment is to create a function with a "for" loop that adds or multiplies elements of the arrays. The inputs are X and Y, but since they aren't defined I think it's screwing up my code. I believe we are supposed to load the first function so the next function (two input two output) can grab the X and Y from memory, perform the operations and output m and b (this is for a linear regression). (fyi-We're not supposed to use the 'sum' function, gotta use loops)
eta: And how do I create a single column vector with 1 to n elements? Rows is easy but I couldn't figure out a column.

 Accepted Answer

[X,Y] = myfunct % try calling it with two outputs
function [X,Y] = myfunct
X=[1;2;3];
Y=[1;2;3];
plot(X,Y,'.b')
end

10 Comments

[X,Y] = myfunct %function calling at the beginning
[sumx,sumy] = myregret(X,Y)
G = X.'*X
H = X.'*Y
I = vpa(sumx.*sumy,4)
function [X,Y] = myfunct()
X=[1;2;3;4;5;6;7;8;9;10;11;12;13;14;15]
Y=[50;49;46;45.5;46;45;42.9;41.5;39;35.6;31;27.5;23;16.3;10]
plot(X,Y,'.b')
end
function [sumx,sumy] = myregret(X,Y)
n=length(X);
sumx = 0 ;
sumy = 0;
for i=2:n
sumx(i) = sumx(i-1) + X(i);
sumy(i) = sumy(i-1) + Y(i);
end
end
myfunct returns the value of X and Y which is passed onto the next function as argument to calculate sums and sumy
[X,Y] = myfunct
Calls the function myfunct. Internally no matter what that function calls its variables, the first positional output from the function will be assigned to X in the workspace that is calling the function, and the second positional output will be assigned to Y. As far as the caller of myfunct is concerned, it would be perfectly fine if myfunct were defined by
function [SeaMonkey, unobtanium] = myfunct
unobtanium = randi(10, 1, 25) ;
SeaMonkey = sqrt(unobtanium + 7);
Because there is no connection between the names of the variables used in the function and the variables assigned to in the caller, since it is all positional.
Thank you sir Walter
"sorry if I sound stupid but by "Calls" you mean 'asks the computer' and not 'gives the name'?"
"Calling" a function is basically when you tell MATLAB to run a function, by typing its name with the required inputs and outputs. Functions can be called:
  1. by typing it in the command window and then pressing enter, or
  2. by writing it in a script/function/... and then running that script/function/....
When you call MATLAB's own functions it is exactly the same, e.g. in the command window:
>> y = sin(0) % call function SIN with one input and one output.
y = 0
You do the same with your function (with the required input/output arguments):
>> [A,B] = myfunct()
As Walter Roberson already noted, the names used inside the function are irrelevant to what names you use for the input/output arguments when the function is called. Do we know the names that SIN uses internally in its calculations? No, we don't. The internal names are irrelevant. The same applies to your function when you call it.
How to call functions is explained in the introductory tutorials:
It just tells myfunct to return to output arguments and assign them to variables called A and B in the calling workspace.
A function is just like a black box with a bunch of things going in and a bunch of things coming out. There may be none going in or none going out and of those that are returned by the function (as given by your function definition line) you can choose to ignore as many as you wish, either by just not referring to them if they are at the end or by replacing them with ~ if they are earlier in the list and you want a later output e.g.
[X,Y] = myfunct( ); % Return 2 arguments, pass in 0 input arguments
X = myfunct( ); % Return only the first output argument and ignore any subsequent ones
[~,Y] = myfunct( ); % Ignore the first output but return the second (and ignore any that may come after the second).
getData??
regressConst change it to regressConst1
Note: when you have a function defined in a script file, the name of the script file cannot be the same as the name of the function.
Hi All, thanks for the help. I went through my code and finally got it to do what I wanted. It seems I was calling for the previous function outside the new function and that was screwing it up. Lesson learned, I hope. :)
You can show your thanks to everyone by accepting the answer

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!