How to draw the activation record of the following code - language-agnostic

I am having trouble drawing the activation record for the procedure Third. The language is assumed to be stack based and parameter passing is by value-result.
'''
program First
var A, B, C: real;
A = B = C = 1.0;
procedure Second;
var B, C, D : integer;
B = C = D = 2;
Call Third( C, D);
end Second
procedure Third(X, Y: integer)
var C, F, G : integer;
C = F = G = 3;
C:= A + Y;
end Third
call Second();
end First
'''
I understand that an activation record consists of a pointer, a return value and address, and parameters and I understand that passing by value-result creates a copy of a variable before a procedure and reassigns it to the return value after a procedure. However, I am confused how to align the procedure Third to the activation record and I am unsure what ":=" refers to in this context.

Related

Matlab - Undefined function 'int_f_1' for input arguments of type 'double'

Currently, I am working on a program that integrates x + x^2 + e^x + 2cos(x/2) - 1 with three input variables, a, b, and n. What I need returned is the numerical integral from a to b with n increments. The function also has to return trapezoids for each n as a column vector. Thus, the integral value as a scalar, and a vector of values.
I've gotten to a point where the function int_f_1 is undefined for some reason, and I have no idea why. I thought by nesting that function under the test function, it would help. But it does not, and I don't know why that is. Any suggestions?
function [y] = test_function_1(x);
y = x + x.^2 + exp(x) + 2*cos(x/2) - 1
end
function [int_f, increment] = int_f_1 (a, b, n);
f = #test_function_1;
h = a + b ./ n
increments = h
int_f = integral(h, f)
end

How do I convert Matlab script to function

I wrote my script for a class but the next step is to convert that script into a function. What do I type in?
This is my script:
% Find max volume
b = 2.75; %ft for diameter
h = 3.00; %ft for height
v = (b*h)/3; %ft^3
% use volume to find mass
p = 62.3;
m = p*v;
The following syntax is used to create a function in MATLAB:
function [y1,...,yN] = myfun(x1,...,xM)
In the example below, the maximumVolume() method takes 3 parameters (b, h, p) and returns the value of the m variable.
result = maximumVolume(2.75, 3.0, 62.3);
function m = maximumVolume(b, h, p)
v = (b * h)/3;
m = p * v;
end
References
function

Use varargin for multiple arguments with default values in MATLAB

Is there a way to supply arguments using varargin in MATLAB in the following manner?
Function
func myFunc(varargin)
if a not given as argument
a = 2;
if b not given as argument
b = 2;
if c not given as argument
c = a+b;
d = 2*c;
end
I want to call the above function once with b = 3 and another time while the previous one is running in the same command window with a = 3 and c = 3 and letting b take the default value in the function this time. How can it be done using varargin?
Here's the latest and greatest way to write the function (using arguments blocks from R2019b)
function out = someFcn(options)
arguments
options.A = 3;
options.B = 7;
options.C = [];
end
if isempty(options.C)
options.C = options.A + options.B;
end
out = options.A + options.B + options.C;
end
Note that this syntax does not allow you to say options.C = options.A + options.B directly in the arguments block.
In MATLAB < R2021a, you call this like so
someFcn('A', 3)
In MATLAB >= R2021a, you can use the new name=value syntax
someFcn(B = 7)
Here are two ways to do this which have been available since 2007a (i.e. a long time!). For a much newer approach, see Edric's answer.
Use nargin and ensure your inputs are always in order
Use name-value pairs and an input parser
nargin: slightly simpler but relies on consistent input order
function myFunc( a, b, c )
if nargin < 1 || isempty(a)
a = 2;
end
if nargin < 2 || isempty(b)
b = 2;
end
if nargin < 3 || isempty(c)
c = a + b;
end
end
Using the isempty check you can optionally provide just later arguments, for example myFunc( [], 4 ) would just set b=4 and use the defaults otherwise.
inputParser: more flexible but can't directly handle the c=a+b default
function myFunc( varargin )
p = inputParser;
p.addOptional( 'a', 2 );
p.addOptional( 'b', 2 );
p.addOptional( 'c', NaN ); % Can't default to a+b, default to NaN
p.parse( varargin{:} );
a = p.Results.a;
b = p.Results.b;
c = p.Results.c;
if isnan(c) % Handle the defaulted case
c = a + b;
end
end
This would get used like myFunc( 'b', 4 );. This approach is also agnostic to the input order because of the name-value pairs, so you can also do something like myFunc( 'c', 3, 'a', 1 );

Function with vector as argument in Octave

How can I make a function with a vector as input and a matrix as an output?
I have to write a function that will convert cubic meters to liters and English gallons. The input should be a vector containing volume values ​​in m ^ 3 to be converted. The result should be a matrix in which the first column contains the result in m ^ 3, the second liter, the third English gallon.
I tried this:
function [liter, gallon] = function1 (x=[a, b, c, d]);
liter= a-10+d-c;
gallon= b+15+c;
endfunction
You're almost there.
The x=[a,b,c,d] part is superfluous, your argument should be just x.
function [liter, gallon] = function1 (x);
a = x(1); b = x(2); c = x(3); d = x(4);
liter = a - 10 + d - c;
gallon = b + 15 + c;
endfunction
If you want your code to be safe and guard against improper inputs, you can perform such checks manually inside the function, e.g.
assert( nargin < 1 || nargin > 4, "Wrong number of inputs supplied");
The syntax x=[a,b,c,d] does not apply to octave; this is reserved for setting up default arguments, in which case a, b, c, and d should be given specific values that you'd want as the defaults. if you had said something like x = [1,2,3,4], then this would be fine, and it would mean that if you called the function without an argument, it would set x up to this default value.

Octave call a function as a variable of another function

I wrote a bisection method in Octave but it can't consume another function..
My bisection method code is like:
function[x,b] = bisection(f,a,b)
t = 10e-8
while abs(b-a) > t;
c = (a+b)/2;
if f(a) * f(b) <= 0
a = a;
b = c;
else
b = b;
a = c
endif
endwhile
x = (a+b)/2
endfunction
And I already have a file f1.m:
function y = f1(x)
y = x^2 - 4;
endfunction
But when I call [x,v] = bisection[f1,0,5], I get:
>> [t,v] = bisection(f1,0,5)
error: 'x' undefined near line 2 column 5
error: called from
f1 at line 2 column 3
error: evaluating argument list element number 1
what you want is to pass a pointer to f1 to your function bisection so the right call would be
[t,v] = bisection(#f1,0,5)
which outputs:
t = 1.0000e-07
a = 0.62500
a = 0.93750
a = 1.0938
a = 1.1719
a = 1.2109
a = 1.2305
a = 1.2402
a = 1.2451
a = 1.2476
a = 1.2488
a = 1.2494
a = 1.2497
a = 1.2498
a = 1.2499
a = 1.2500
a = 1.2500
a = 1.2500
a = 1.2500
a = 1.2500
a = 1.2500
a = 1.2500
a = 1.2500
a = 1.2500
a = 1.2500
x = 1.2500
t = 1.2500
v = 1.2500
Andy has given you the answer on how to fix this. I would just like to add why you get that error and what it means. Consider the following octave session:
octave:1> function Out = g1(x); Out = x+5; end
octave:2> function Out = g2(); Out = 10;end
octave:3>
octave:3> g2
ans = 10
octave:4> g1
error: 'x' undefined near line 1 column 29
error: called from
g1 at line 1 column 27
I.e., when you write g1 or g2 here, this is an actual function call. The call to g2 succeeds because g2 does not take any arguments; the syntax g2 is essentially equivalent to g2(). However, the call to g1 fails, because g1 expects an argument, and we didn't provide one.
Compare with:
octave:4> a = #g1;
octave:5> b = #g2;
octave:6> a
a = #g1
octave:7> a(1)
ans = 6
octave:8> b
b = #g2
octave:9> b()
ans = 10
where you have created handles to these functions, which you can capture into variables, and pass them as arguments into functions. These handles could then be called as a(5) or b() inside the function that received them as arguments, and it would be like calling the original g1 and g2 functions.
When you called bisection(f1,0,5), you essentially called bisection(f1(),0,5), i.e. you asked octave to evaluate the function f1 without passing any arguments, and use the result as the first input argument to the bisection function. Since function f1 is defined to take an input argument and you didn't supply any, octave complains that when it tries to evaluate y = x^2 - 4; as per the definition of f1, x was not passed as an input argument and was therefore undefined.
Therefore, to pass a "function" as an arbitrary argument that can be called inside your bisection function, you need to pass a function handle instead, which can be created using the #f1 syntax. Read up on "anonymous functions" on the octave (or matlab) documentation.