My code in octave seemed has a problem with the code error - octave

I was trying to run the program but the window says
error: 'a' undefined near line 2, column 10
error: called from
false_position at line 2 column 6
here's my code
function y = false_position(f, a, b, error)
if ~(f(a) < 0)
disp("f(a) must be less than 0")
elseif ~(f(b) > 0)
disp("f(b) must be greater than zero")
else
c = 100000;
while abs(f(c)) > error
%Formula for the x-intercept
c = -f(b) * (b - a) / (f(b) - f(a)) + b;
if f(c) < 0
a = c;
else
b = c;
endif
disp(f(c))
endwhile
x = ["The root is approximately located at ", num2str(c)];
disp(x)
y = c;
endif
endfunction
every time i ran the code, it says like that and i am not really a pro with using the octave. I was kinda hoping someone will help me with this error.
Any answers will do

Related

Simulating waves in disc or circle form

I am getting an error when I run this code while selecting disc view or circle view option for wave simulation. The code and error are attached. I think there is some problem in this part of code typically in fzero function. Any help would be great.
Code:
function z = bjzeros(n,k)
% BJZEROS Zeros of the Bessel function.
% z = bjzeros(n,k) is the first k zeros of besselj(n,x)
% delta must be chosen so that the linear search can take
% steps as large as possible
delta = .99*pi;
Jsubn = inline('besselj(n,x)''x','n');
a = n+1;
fa = besselj(n,a);
z = zeros(1,k);
j = 0;
while j < k
b = a + delta;
fb = besselj(n,b);
if sign(fb) ~= sign(fa)
j = j+1;
z(j) = fzerotx(Jsubn,[a b],n);
end
a = b;
fa = fb;
end
Error:
Undefined function 'fzerotx' for input arguments of type 'inline'.
Error in waves>bjzeros (line 292)
z(j) = fzerotx(Jsubn,[a b],n);
Error in waves (line 137)
mu = [bjzeros(0,2) bjzeros(1,2)];
Function Declarations and Syntax
The fzerotx() function may not be declared. You can follow the file structure below to create the required M-files/functions in the same directory. Another small error may be caused by a missing comma, I got rid of the error by changing the line:
Jsubn = inline('besselj(n,x)''x','n');
to
Jsubn = inline('besselj(n,x)','x','n');
File 1: Main File/Function Call → [main.m]
mu = [bjzeros(0,2) bjzeros(1,2)];
File 2: bjzeros() Function → [bjzeros.m]
function z = bjzeros(n,k)
% BJZEROS Zeros of the Bessel function.
% z = bjzeros(n,k) is the first k zeros of besselj(n,x)
% delta must be chosen so that the linear search can take
% steps as large as possible
delta = .99*pi;
Jsubn = inline('besselj(n,x)','x','n');
a = n+1;
fa = besselj(n,a);
z = zeros(1,k);
j = 0;
while j < k
b = a + delta;
fb = besselj(n,b);
if sign(fb) ~= sign(fa)
j = j+1;
z(j) = fzerotx(Jsubn,[a b],n);
end
a = b;
fa = fb;
end
end
File 3: fzerotx() Function → [fzerotx.m]
Function Reference: MATLAB: Textbook version of FZERO
function b = fzerotx(F,ab,varargin)
%FZEROTX Textbook version of FZERO.
% x = fzerotx(F,[a,b]) tries to find a zero of F(x) between a and b.
% F(a) and F(b) must have opposite signs. fzerotx returns one
% end point of a small subinterval of [a,b] where F changes sign.
% Arguments beyond the first two, fzerotx(F,[a,b],p1,p2,...),
% are passed on, F(x,p1,p2,..).
%
% Examples:
% fzerotx(#sin,[1,4])
% F = #(x) sin(x); fzerotx(F,[1,4])
% Copyright 2014 Cleve Moler
% Copyright 2014 The MathWorks, Inc.
% Initialize.
a = ab(1);
b = ab(2);
fa = F(a,varargin{:});
fb = F(b,varargin{:});
if sign(fa) == sign(fb)
error('Function must change sign on the interval')
end
c = a;
fc = fa;
d = b - c;
e = d;
% Main loop, exit from middle of the loop
while fb ~= 0
% The three current points, a, b, and c, satisfy:
% f(x) changes sign between a and b.
% abs(f(b)) <= abs(f(a)).
% c = previous b, so c might = a.
% The next point is chosen from
% Bisection point, (a+b)/2.
% Secant point determined by b and c.
% Inverse quadratic interpolation point determined
% by a, b, and c if they are distinct.
if sign(fa) == sign(fb)
a = c; fa = fc;
d = b - c; e = d;
end
if abs(fa) < abs(fb)
c = b; b = a; a = c;
fc = fb; fb = fa; fa = fc;
end
% Convergence test and possible exit
m = 0.5*(a - b);
tol = 2.0*eps*max(abs(b),1.0);
if (abs(m) <= tol) | (fb == 0.0)
break
end
% Choose bisection or interpolation
if (abs(e) < tol) | (abs(fc) <= abs(fb))
% Bisection
d = m;
e = m;
else
% Interpolation
s = fb/fc;
if (a == c)
% Linear interpolation (secant)
p = 2.0*m*s;
q = 1.0 - s;
else
% Inverse quadratic interpolation
q = fc/fa;
r = fb/fa;
p = s*(2.0*m*q*(q - r) - (b - c)*(r - 1.0));
q = (q - 1.0)*(r - 1.0)*(s - 1.0);
end;
if p > 0, q = -q; else p = -p; end;
% Is interpolated point acceptable
if (2.0*p < 3.0*m*q - abs(tol*q)) & (p < abs(0.5*e*q))
e = d;
d = p/q;
else
d = m;
e = m;
end;
end
% Next point
c = b;
fc = fb;
if abs(d) > tol
b = b + d;
else
b = b - sign(b-a)*tol;
end
fb = F(b,varargin{:});
end
Ran using MATLAB R2019b

The not operator(!) is not working as expected in Octave. What could be the reason?

In the following octave code, the condition in the while statement is not being evaluated as expected.
The condition in the while statement is always evaluating as "TRUE" even if the value of "stop_training" is being printed as the number 0.
What could be wrong with the statement?
stop_training = 0;
C = 1;
while (! stop_training) %NOT operator is not working
C = input("Enter the value for C:");
if(length(C)==0)
C=1;
endif
fprintf("Using C = %d\n", C);
A = C+1;
stop_training = yes_or_no("continue to train the model?");
fprintf("you chose: %d\n", stop_training);
pause;
endwhile

Piecewise Functions in R

I'm trying to plot a piecewise function in R that is equal to -3 if x < 0, 1/3 * x^3 if x is between 0 and 5 (inclusive), and 4x otherwise. The function I've been running seems to work, but the plot returns an error message. The code I wrote is:
g <- function(x) {
if (x < 0)
-3 # first component
else if (x >= 0 & x <= 5)
(1/3) * x #second component
else
4*x # third component
}
Then when I try to plot it with
plot(g, -20, 20)
I get
Error in curve(expr = x, from = from, to = to, xlim = xlim, ylab = ylab, :
'expr' did not evaluate to an object of length 'n'
In addition: Warning message:
In if (x < 0) -3 else if (x >= 0 & x <= 5) (1/3) * x else 4 * x :
the condition has length > 1 and only the first element will be used
I can't quite figure out how to troubleshoot this, and the 'ifelse' function returned a dark black line when I attempted to plot it, which I'd prefer to avoid, so this seems to be my best bet.
I would greatly appreciate any insights.

Octave variable definition error

I'm trying to generate Blackman-Harris window coefficients in Octave. I have declared a function in a .m file like so:
function result = BlackmanHarris(window_size)
a0 = 0.35875
a1 = 0.48829
a2 = 0.14128
a3 = 0.01168
result = [0:window_size - 1];
if(nargin != 1)
print_usage("BlackmanHarris(int window_size)");
endif
if(isinteger(window_size))
for n = 0:window_size - 1
result(n) = a0 - (a1 * cos((2 * pi * n)/(window_size - 1))) + (a2 * cos((4 * pi * n)/(window_size - 1))) - (a3 * cos((6 * pi * n)/(window_size - 1)));
endfor
else
error("BlackmanHarris: Expecting integer argument.");
endif
endfunction
When I attempt to run this, I get the error:
>> window = BlackmanHarris(window_size);
error: 'a0' undefined near line 15 column 16
error: called from
BlackmanHarris at line 15 column 14
I have tried declaring the variables as 'global' and 'persistent', nether of which solve this issue. I'm sure I'm just doing something slightly wrong, but Google has yielded little help on this.
Thanks in advance.
As #Sardar_Usama said, I was attempting to access element 0 of the result array, which of course will not work.
Changing that to n+1 fixed the issue.

I'm trying to do Ackermann's Function on IBasic

Ackermann's Function is a recursive mathematical algorithm that can be used to test how well a computer performs recursion. Design a function ackermann(m,n), which solves Ackermann's Function. Use the following logic in your function:
If m = 0, then return n + 1
If n = 0 then return ackermann(m-1, 1)
Otherwise, return ackermann(m-1, ackermann(m, n-1))
The program stops after it hits 13. Can anyone tell me what I have done wrong?
declare main()
declare ackermann(m:int, n:int)
openconsole
main()
print:print "Press any key to quit...",
do:until inkey$<>""
closeconsole
end
sub main()
def counter, m, n:int
counter = 0
for counter = 1 to 100
print ackermann(counter)
next counter
return
sub ackermann(m, n)
if m = 0
return = n + 1
else
if n = 0
return = ackermann(m - 1, 1)
else
return = ackermann(m - 1, ackermann(m, n - 1))
endif
endif
return
Your print statement is
print ackermann(counter)
but your function is
sub ackermann(m, n)
You need to send a second parameter in the first call.
Do note that Ackermann's function grows incredibly quickly - values above m,n > (3,4) are going to have a LOT of digits and if you go more than even about (4,4) you'll quickly find numbers that could quite possibly fill up your memory entirely with digits...
Refer to Wikipedia to see the magnitude of the numbers you are trying to compute...
declare main()
declare ackermann(m:int, n:int)
openconsole
main()
print:print "Press any key to quit...",
do:until inkey$<>""
closeconsole
end
sub main()
print Ackermann(3,5)
return
sub ackermann(m, n)
if m = 0
return = n + 1
else
if n = 0
return = ackermann(m - 1, 1)
else
return = ackermann(m - 1, ackermann(m, n - 1))
endif
endif
return