Using Hoare-Rules to show PRECONDITION implies POSTCONDITION in a simple program (just 2 assignments) - proof

Using the Hoare-Rules I want to show that I can imply
{x >= 0} --> {a + y = x}
PROGRAMM
// PRECONDITION
{x >= 0}
a = x;
y = 0;
// POSTCONDITION
{a + y = x}
Using the assignment rules I get
// PRECONDITION
{x >= 0}
{x + 0 = x} // assignment rule
a = x;
{a + 0 = x} // assignment rule
y = 0;
// POSTCONDITION
{a + y = x}
To show
{x >= 0} --> {a + y = x}
therefore I need to show in a last step
{x >= 0} --> {x + 0 = x}
How can I show this or what is wrong in my proof?

Your reasoning is correct.
To prove an implication formally, argue as follows:
Assume the antecedent, x >= 0
By Additive identity, we have x + 0 = x
By implication introduction (from 1 and 2) we have x >= 0 --> x + 0 = x

Related

how to call a function to another function in julia?

I am writing a code in julia but I am unable to call a function from another function. Code is:
function add(x, y)
if x == 3 && y ==1
z =0
else x == 0 && y ==0
z =1
end
return z
end
function width(a, b, c)
add(x,y)
.....
end
The variables in add function will be used in width function but as I am new to julia, I am unable to call add in the other function. Kindly guide.
Edit:
I tried declaring with the z but it also didn't worked
struct z
a::Int
b::Int
end
There are two problems in your code that are not related to Julia per se. First problem in the add function: if x == 3 && y == 1 the output should be z = 0, else if x == 0 && y == 0, actually the if was missing, the output should be z = 1. Now what will be the output if, e.g., x = 1 && y == 1? The answer is nothing and z will be undefined.
To fix the add function, you should add a default branch for the if-else.
function add(x, y)
if x == 3 && y == 1
z = 0
elseif x == 0 && y == 0
z = 1
else
z = -1 # some default
end
return z
end
The same function could be written more concisely as:
function add(x, y)
x == 3 && y == 1 && return 0
x == 0 && y == 0 && return 1
return -1 # some default
end
which can even be written in a one-liner like this:
add(x, y) = x == 3 && y == 1 ? 0 : x == 0 && y == 0 ? 1 : -1 # some default
The second problem is with the width function. x and y are not defined inside the body of the width function. So, you can't call add(x, y). It should be z = add(a, b) where z should be used in subsequent calculations. Finally, check what the third argument c is for, otherwise, remove it.
function width(a, b, c)
z = add(a, b)
.....
end

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

Implementing Euler's Method in GNU Octave

I am reading "Numerical Methods for Engineers" by Chapra and Canale. In it, they've provided pseudocode for the implementation of Euler's method (for solving ordinary differential equations). Here is the pseucode:
Pseucode for implementing Euler's method
I tried implementing this code in GNU Octave, but depending on the input values, I am getting one of two errors:
The program doesn't give any output at all. I have to press 'Ctrl + C' in order to break execution.
The program gives this message:
error: 'ynew' undefined near line 5 column 21
error: called from
Integrator at line 5 column 9
main at line 18 column 7
I would be very grateful if you could get this program to work for me. I am actually an amateur in GNU Octave. Thank you.
Edit 1: Here is my code. For main.m:
%prompt user
y = input('Initial value of y:');
xi = input('Initial value of x:');
xf = input('Final value of x:');
dx = input('Step size:');
xout = input('Output interval:');
x = xi;
m = 0;
xpm = x;
ypm = y;
while(1)
xend = x + xout;
if xend > xf
xend = xf;
h = dx;
Integrator(x,y,h,xend);
m = m + 1;
xpm = x;
ypm = y;
if x >= xf
break;
endif
endif
end
For Integrator.m:
function Integrator(x,y,h,xend)
while(1)
if xend - x < h
h = xend - x;
Euler(x,y,h,ynew);
y = ynew;
if x >= xend
break;
endif
endif
end
endfunction
For Euler.m:
function Euler(x,y,h,ynew)
Derivs(x,y,dydx);
ynew = y + dydx * h;
x = x + h;
endfunction
For Derivs.m:
function Derivs(x,y,dydx)
dydx = -2 * x^3 + 12 * x^2 - 20 * x + 8.5;
endfunction
Edit 2: I shoud mention that the differential equation which Chapra and Canale have given as an example is:
y'(x) = -2 * x^3 + 12 * x^2 - 20 * x + 8.5
That is why the 'Derivs.m' script shows dydx to be this particular polynomial.
Here is my final code. It has four different M-files:
main.m
%prompt the user
y = input('Initial value of y:');
x = input('Initial value of x:');
xf = input('Final value of x:');
dx = input('Step size:');
xout = dx;
%boring calculations
m = 1;
xp = [x];
yp = [y];
while x < xf
[x,y] = Integrator(x,y,dx,min(xf, x+xout));
m = m+1;
xp(m) = x;
yp(m) = y;
end
%plot the final result
plot(xp,yp);
title('Solution using Euler Method');
ylabel('Dependent variable (y)');
xlabel('Independent variable (x)');
grid on;
Integrator.m
%This function takes in 4 inputs (x,y,h,xend) and returns 2 outputs [x,y]
function [x,y] = Integrator(x,y,h,xend)
while x < xend
h = min(h, xend-x);
[x,y] = Euler(x,y,h);
end
endfunction
Euler.m
%This function takes in 3 inputs (x,y,h) and returns 2 outputs [x,ynew]
function [x,ynew] = Euler(x,y,h)
dydx = Derivs(x,y);
ynew = y + dydx * h;
x = x + h;
endfunction
Derivs.m
%This function takes in 2 inputs (x,y) and returns 1 output [dydx]
function [dydx] = Derivs(x,y)
dydx = -2 * x^3 + 12 * x^2 - 20 * x + 8.5;
endfunction
Your functions should look like
function [x, y] = Integrator(x,y,h,xend)
while x < xend
h = min(h, xend-x)
[x,y] = Euler(x,y,h);
end%while
end%function
as an example. Depending on what you want to do with the result, your main loop might need to collect all the results from the single steps. One variant for that is
m = 1;
xp = [x];
yp = [y];
while x < xf
[x,y] = Integrator(x,y,dx,min(xf, x+xout));
m = m+1;
xp(m) = x;
yp(m) = y;
end%while

Prolog, predicate that swaps 1 to 0 and 0 to 1 and puts them in a list

New to Prolog.
I want a program that swaps 1 to 0 and 0 to 1 and answers that question:
?- swap([1,1,0,1,0,0,0,1], L2).
L2 = [0,0,1,0,1,1,1,0]
complement(0, 1).
complement(1, 0).
swap(X, Y) :- maplist(complement, X, Y).
A CLPFD solution that works properly in all directions, for reference:
:- use_module(library(clpfd)).
swap(X, Y) :-
maplist(#\=, X, Y),
X ins 0..1,
Y ins 0..1.
Specifically:
?- swap(X,Y).
X = Y, Y = [] ;
X = [_2640],
Y = [_2658],
_2640 in 0..1,
_2640#\=_2658,
_2658 in 0..1 ;
X = [_3200, _3206],
Y = [_3224, _3230],
_3200 in 0..1,
_3200#\=_3224,
_3224 in 0..1,
_3206 in 0..1,
_3206#\=_3230,
_3230 in 0..1 ;
…
works properly, when swap/2 from the other answer does not:
?- swap2(X,Y).
X = Y, Y = [] ;
X = [0],
Y = [1] ;
X = [0, 0], % Doesn't enumerate ([1],[0]), etc.
Y = [1, 1] ;
X = [0, 0, 0],
Y = [1, 1, 1] ;
…

Recursive function in Prolog

I try to calculate recursive function:
f(x, y) = 0 if x = 0;
f(x, y) = 1 if x = 1;
f(x, y) = y*y*f(x-2,y) if x>1.
I tried this way:
f(0,_,0).
f(1,_,1).
f(X,Y,Z):-
X>1,
XX is X-2,
ZZ = Y*Y*Z,
f(XX,Y,ZZ).
I can only obtain true/false. How can I calculate value of this function?
Thank you very much!
You can easily turn this into a true relation if you simply use CLP(FD) constraints:
:- use_module(library(clpfd)).
f(0, _, 0).
f(1, _, 1).
f(X, Y, Z):-
X #> 1,
XX #= X-2,
Z #= Y*Y*ZZ,
f(XX, Y, ZZ).
Example query and its result:
?- f(X, 5, Z).
X = Z, Z = 0 ;
X = Z, Z = 1 ;
X = 2, Z = 0 ;
X = 3, Z = 25 .
Your last rule looks a little odd to me. I'd try something like this:
f(X,Y,Z) :-
X > 1,
XX is X-2,
f(XX,Y,ZZ),
Z is Y * Y * ZZ.