What is = followed by == operation? - octave

I've encountered this code and don't know what its performing :
yk = y == k;
Recreating :
>> a = 1
a = 1
>> b = 2
b = 2
>> c = 3
c = 3
>> a = b == c
a = 0
>>
I think it is a boolean operation. If y == k then yk = 1 else yk = 0 ?

In order to figure out what your statement means, you can refer to Octave's operator precedence. As you can see from that list, assignment (=) has the lowest precedence of any operator (including ==). As a result, the line that you have posted translates to
Perform the relational operation y == k
Assign the result to the variable yk

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

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.

Using nested functions to find product of numbers

I need to make a function that given natural number n, calculates the product
of the numbers below n that are not divisible by
2 or by 3 im confused on how to use nested functions in order to solve this problem (also new to sml ) here is my code so far
fun countdown(x : int) =
if x=0
then []
else x :: countdown(x-1)
fun check(countdown : int list) =
if null countdown
then 0
else
It is not clear from the question itself (part of an exercise in some class?) how we are supposed to use nested functions since there are ways to write the function without nesting, for example like
fun p1 n =
if n = 1 then 1 else
let val m = n - 1
in (if m mod 2 = 0 orelse m mod 3 = 0 then 1 else m) * p1 m
end
and there are also many ways to write it with nested functions, like
fun p2 n =
if n = 1 then 1 else
let val m = n - 1
fun check m = (m mod 2 = 0 orelse m mod 3 = 0)
in (if check m then 1 else m) * p2 m
end
or
fun p3 n =
let fun check m = (m mod 2 = 0 orelse m mod 3 = 0)
fun loop m =
if m = n then 1 else
(if check m then 1 else m) * loop (m + 1)
in loop 1
end
or like the previous answer by #coder, just to give a few examples. Of these, p3 is somewhat special in that the inner function loop has a "free variable" n, which refers to a parameter of the outer p3.
Using the standard library, a function that produces the numbers [1; n-1],
fun below n = List.tabulate (n-1, fn i => i+1);
a function that removes numbers divisible by 2 or 3,
val filter23 = List.filter (fn i => i mod 2 <> 0 andalso i mod 3 <> 0)
a function that calculates the product of its input,
val product = List.foldl op* 1
and sticking them all together,
val f = product o filter23 o below
This generates a list, filters it and collapses it. This wastes more memory than necessary. It would be more efficient to do what #FPstudent and #coder do and generate the numbers and immediately either make them a part of the end product, or throw them away if they're divisible by 2 or 3. Two things you could do in addition to this is,
Make the function tail-recursive, so it uses less stack space.
Generalise the iteration / folding into a common pattern.
For example,
fun folditer f e i j =
if i < j
then folditer f (f (i, e)) (i+1) j
else e
fun accept i = i mod 2 <> 0 andalso i mod 3 <> 0
val f = folditer (fn (i, acc) => if accept i then i*acc else acc) 1 1
This is similar to Python's xrange.

Call comparison operators in Haskell

i need to find out if the difference from differenceAB is the smallest:
smallestDifference3 :: Int -> Int -> Int -> Int
smallestDifference a b c
| differenceAB < differenceBC < differenceAC = differenceAB
| otherwise = differenceAB
where differenceAB
| a < b = -(a - b)
| otherwise = a - b
differenceBC
| b < c = -(b - c)
| otherwise = b - c
differenceAC
| a < c = -(a - c)
| otherwise = a - c
but i get this error:
cannot mix `<' [infix 4] and `<' [infix 4] in the same infix expression
how to solve my problem? anybody know´s? greetingS!
There are a couple of problems here.
You have 3 exactly identical functions. Probably you want values:
smallestDifference a b c = ....
where
diffAC = abs(a-c)
diffAB = abs(a-b)
diffBC = abs(b-c)
Now for the expression, you can't write
diffAC < diffAB < diffBC
since (<) is a non-associative operator. Which means you must write explicit parentheses:
(diffAC < diffAB) < diffBC
But this doesnt type check, because for the second (<) the left hand side is Bool, but the right hand side is Int. What you want is
(diffAC < diffAB) && (diffAB < diffBC)
i.e. if ac is lower then ab and ab is lower than bc
If you just want to find the smallest difference, following will work.
smallestdiff a b c = minimum [abs $ a-b, abs $ b-c, abs $a-c]
There is no such thing as chaining operators in Haskell. You should use explicit logical operations:
smallestDifference3 :: Int -> Int -> Int -> Int
smallestDifference a b c
| (differenceAB < differenceBC) && (differenceBC < differenceAC) = differenceAB
| otherwise = differenceAB
BTW, your code is weird, you return differenceAB from both guard clauses. It is not clear what you want to achieve, so I cannot help further.

Mysql Precedence Logic

Any explanation to the following queries :
Select x FROM y WHERE a = 1 OR a = 2 AND (b = 1 OR b = 2)
why it doesn't return the correct info while this return the correct info :
Select x FROM y WHERE (a = 1 OR a = 2) AND (b = 1 OR b = 2)
Am i missing something here ?
X Y (X OR Y) X OR Y
1 0 1 1
0 1 1 1
1 1 1 1
0 0 0 0
I know in term of precedence the () have priority , but why should i add them the the first part of the query ?
Correct me if I'm wrong
Thank you
AND has a higher precedence than OR so your first query is equivalent to this:
Select x FROM y WHERE a = 1 OR a = 3 OR (a = 2 AND (b = 1 OR b = 2))
Which is not equivalent to
Select x FROM y WHERE (a = 1 OR a = 2 OR a = 3) AND (b = 1 OR b = 2)
I guess you forgot the a = 3 part in your first query.
Operator precedence in MySQL
Because ambiguity is an undesirable trait?
Also, the optimizer will re-order your WHERE Conditions if it thinks it will perform better. Your ambiguity will, therefore, cause different results depending on how/what it evaluates first.
Always be explicit with your intentions.
Using parentheses in your WHERE clause does not just affect precedence but also groups predicates together. In your example the difference in results is more a matter of grouping rather than precedence.
You could think of this: (pN = predicate expression)
WHERE a = 1 OR a = 2 AND (b = 1 OR b = 2)
as:
WHERE p1 OR p2 AND p3
And this:
WHERE (a = 1 OR a = 2 OR a = 3) AND (b = 1 OR b = 2)
as:
WHERE p1 AND p2
and so it becomes clear that the results could be quite different.