Given these set of points, what would be the mathematical function for this and the Big(O) notation? - function

X=2, y=1
X=3, y=3
X=4, y= 6
X=5, y= 10
X=6, y= 15
X=7, y= 21
X=8, y=28
I know that f(x) = f(x-1) + (x-1)
But...is that the correct mathematical function? What would Big O notation be?

The correct (or at least, significantly more efficient than recursive) equation would be
f(x) = x * (x - 1) / 2

Looks like homework. You should mark it with the homework tag.
Did you mean f(x) = f(x-1) + (x-1) ?
To solve for the function:
http://en.wikipedia.org/wiki/Recurrence_relation#Solving
To get the complexity:
http://en.wikipedia.org/wiki/Master_theorem

Yes the function is right, the difference between y values is incrementally increasing by 1
Edited: Thanks for the comment by trutheality
For complexity of the function you can see y like this
y= 1 + (1+2) + (1+2+3) + ....(1+2+3+..n)
As highest possible degree term 1+2+3...n is O(n^2)
y=O(n^2)

The way to correctly state the problem is:
f(x) = f(x - 1) + (x - 1)
f(1) = 0
You want to solve f(x) in terms of x.
There are many ways to solve these kinds of recursive formulas. I like to use Wolfram Alpha, it has an easy interface.
Wolfram Alpha query "f(x)=f(x-1)+(x-1)"
That gives you the precise answer, in big-O notation you would say the function f is in O(x^2).

Related

How to write more than 100 differential equation in MATLAB ode function and to solve it?

I want to solve 100 differential equation by ode45. My entire 100 ode equation has same pattern i.e. in the form of xdot= Ax + By + Cu where xdot, A, B and C have n1 order. So the first equation is xdot(1)=A(1)*x(1) + B(1)*y(1) +C(1)*u(1) and nth equation is xdot(n)= A(n)*x(n) + B(n)*y(n) +C(n)*u(n). It is very hectic to write this 100 equation in function script file of ode MATLAB. My question is that how to write this hundred equation using for loop or any other methods in MATLAB function script and solve the ode equation ?
You can define your system using matrices instead of a list of ODE's.
Given your vectors a, b and c
A = diag(a); B = diag(b); C = diag(c);
xdot = #(x, y, u) A*x + B*y + C*u;
then solve xdot using some solver like ode45.
Edit
Of course you can also use dot-notation. I initially wrote my answer in matrix form due to that being the standard in linear algebra.
xdot = #(x, y, u) a.*x + b.*y + c.*u;

How to plot a 2d Function in MATLAB

I am trying to plot a simple equation in MATLAB.
The equation is
z = x^2 - y^2, for -3 <= x <= 3, -3 <= y <= 3.
The current code that I have is
x = -3:3;
y = -3:3;
z = (x.^2) - (y.^2);
plot(z)
The result is
Please help me in this case because I am not sure if the code and graph is correct. Thank you very much.
This is not a piecewise function. A Piecewise Function is a function defined by multiple sub-functions, where each sub-function applies to a different interval in the domain. There is only one function here that takes two arrays of the same length. The calculations yield a vector of zeros, due to the input arrays. If you change either one of the vectors, that is "x" or "y", you will see a nonzero plot. Your code works as expected.
There is a lot going wrong here: Let's start at the beginning:
x = -3:3;
y = -3:3;
If we evaluate these they both will return an vector of integers:
x =
-3 -2 -1 0 1 2 3
This means that the grid on which the function is evaluated is going to be very coarse. To alleviate this you can define a step size, e.g. x = 3:0.1:3 or use linspace, in which case you set the number of samples, so e.g. x = linspace(-3, 3, 500). Now consider the next line:
z = (x.^2) - (y.^2);
If we evaluate this we get
z =
0 0 0 0 0 0 0
and you plot this vector with the 2d-plotting function
plot(z)
which perfectly explains why you get a straight line. This is because the automatic broadcasting of the arithmetic operators like minuse (-) just subtracts values entry-wise. You however want to evaluate z for each possible pair of values of x and y. To do this and to get a nice plot later you should use meshgrid, and use a plotting function like mesh to plot it. So I'd recommend using
[X,Y] = meshgrid(x,y);
to create the grid and then evaluate the function on the grid as follows
Z = X.^2 - Y.^2;
and finally plot your function with
mesh(X,Y,Z);

Plotting decision boundary line in Octave

I have been working on a machine learning course and currently on Classification. I implemented the classification algorithm and obtained the parameters as well as the cost. The assignment already has a function for plotting the decision boundary and it worked but I was trying to read their code and cannot understand these lines.
plot_x = [min(X(:,2))-2, max(X(:,2))+2];
% Calculate the decision boundary line
plot_y = (-1./theta(3)).*(theta(2).*plot_x + theta(1));
Anyone explain?
I'm also taking the same course as you. I guess what the code does is to generate two points on the decision line.
As you know you have the function:
theta0 + theta1 * x1 + theta2 * x2 = 0
Which it can be rewritten as:
c + mx + ky = 0
where x and y are the axis corresponding to x1 and x2, c is theta(0) or the y-intercept, m is the slope or theta(1), and k is theta(2).
This equation (c + mx + ky = 0) corresponds to the decision boundary, so the code is finding two values for x (or x1) which cover the whole dataset (-2 and +2 in plot_x min and max functions) and then uses the equation to find the corresponding y (or x2) values. Finally, a decision boundary can be plotted -- plot(plot_x, plot_y).
In other words, what it does is to use the the equation to generate two points to plot the line on graph, the reason of doing this is that Octave cannot plot the line given an equation to it.
Hope this can help you, sorry for any mistake in grammar or unclear explanation ^.^
Rearranging equations helped me, so adding those here:
plot_y = -1/theta2 (theta1*plot_x + theta0)
note that index in Octave starts at 1, not at 0, so theta(3) = theta2, theta(2) = theta1 and theta(1) = theta0.
This plot_y equation is equivalent to:
c + mx + ky = 0 <=>
-ky = mx + c <=>
y = -1/k (mx + c)

Defining a Differential Equation in Octave

I am attempting to use Octave to solve for a differential equation using Euler's method.
The Euler method was given to me (and is correct), which works for the given Initial Value Problem,
y*y'' + (y')^2 + 1 = 0; y(1) = 1;
That initial value problem is defined in the following Octave function:
function [YDOT] = f(t, Y)
YDOT(1) = Y(2);
YDOT(2) = -(1 + Y(2)^2)/Y(1);
The question I have is about this function definition. Why is YDOT(1) != 1? What is Y(2)?
I have not found any documentation on the definition of a function using function [YDOT] instead of simply function YDOT, and I would appreciate any clarification on what the Octave code is doing.
First things first: You have a (non linear) differential equation of order two which will require you to have two initial conditions. Thus the given information from above is not enough.
The following is defined for further explanations: A==B means A is identical to B; A=>B means B follows from A.
It seems you are mixing a few things. The guy who gave you the files rewrote the equation in the following way:
y*y'' + (y')^2 + 1 = 0; y(1) = 1; | (I) y := y1 & (II) y' := y2
(I) & (II)=>(III): y' = y2 = y1' | y2==Y(2) & y1'==YDOT(1)
Ocatve is "matrix/vector oriented" so we are writing everything in vectors or matrices. Rather writing y1=alpha and y2=beta we are writing y=[alpha; beta] where y(1)==y1=alpha and y(2)==y2=beta. You will soon realize the tremendous advantage of using especially this mathematical formalization for ALL of your problems.
(III) & f(t,Y)=>(IV): y2' == YDOT(2) = y'' = (-1 -(y')^2) / y
Now recall what is y' and y from the definitions in (I) and (II)!
y' = y2 == Y(2) & y = y1 == Y(1)
So we can rewrite equation (IV)
(IV): y2' == YDOT(2) = (-1 -(y')^2) / y == -(1 + Y(2)^2)/Y(1)
So from equation (III) and (IV) we can derive what you already know:
YDOT(1) = Y(2)
YDOT(2) = -(1 + Y(2)^2)/Y(1)
These equations are passed to the solver. Differential equations of all types are solved numerically by retrieving the "next" value in a near neighborhood to some "previously known" value. (The step size inside this neighborhood is one of the key questions when writing solvers!) So your solver uses your initial condition Y(1)==y(1)=1 to make the next step and calculate the "next" value. So right at the start YDOT(1)=Y(2)==y(2) but you didn't tell us this value! But from then on YDOT(1) is varied by the solver in dependency to the function shape to solve your problem and give you ONE unique y(t) solution.
It seems you are using Octave for the first time so let's make a last comment on function [YDOT] = f(t, Y). In general a function is defined in this way:
function[retVal1, retVal2, ...] = myArbitraryName(arg1, arg2, ...)
Where retVal is the return value or output and arg is the argument or input.

Convert 0 to 1 and Vice Versa

I was asked in an interview : how to convert 0 to 1 and 1 to 0. I answered :
Simple if and switch
Bit flipping.
Are there any other approach?
Simple arithmetic:
x = 1 - x;
Actually, there are an infinite number of polynomials that will map 1 to 0 and vice versa. For example:
x = x * x * x * x * x - x * x * x * x + x * x - 2 * x + 1;
A few obvious possibilities:
!n
1-n
n^1
n==0
n!=1
n<1
Lookup table:
int[] swap = { 1, 0 };
And later:
x = swap[x];
Take a paper clip. Straighten it out. It's a 1. Bend it to meet its ends. It's a 0. To make it a 1, straighten it.
they probably expected you to use bitwise NOT
Some Trig:
COS(PI * N)^2
In python
import math
math.cos(math.pi * n) ** 2
I can't believe people forgot modulus:
(3 + n) % 2
I guess you could do ABS(VAR - 1) but i think your approaches are more elegant
This one isn't the best, but it works:
pow(0, n);
This should work for any two numbers...
(EDIT: looking at the other answers I may have misread the question... but I still like my answer :-)
public class X
{
public static void main(final String[] argv)
{
int x = Integer.parseInt(argv[0]);
int y = Integer.parseInt(argv[1]);
x += y;
y = x - y;
x = x - y;
System.out.println(x);
System.out.println(y);
}
}
I've used -~-n in JavaScript.
It converts 1 to -1 which is represented as 11111111, then flips the bits to 00000000 which is 0. The second negative sign does not affect the 0. On the other hand, if n is 0, the first negative sign has no effect, the tilde flips the bits, and the second negative sign converts -1 to 1.