How to create a Ulam's distance function in Python? - distance

I am trying to write Ulam's distance function in python from scratch with this given equation, but couldn't figure it out.
Ulam's distance equation
Let me know if you have any solution, thanks

Related

Manhattan distance for K-medoids in matlab

I am trying to use Matlab k-medoids function.
I found these points:
1- k-medoids 'build' function is different than the original 'pam' paper.
2-Matlab doesn't support Manhattan distance(I don't know why).
so
if I use 'sqeuclidean' default distance, the result will be close to Manhattan distance or I have to make my distance function(even I don't know how I can do it?).
link:https://www.mathworks.com/help/stats/kmedoids.html
Thanks

matlab area under Gaussian curve 'trapz(y)' and 'trapz(x,y)' function which is more accurate?

I am very new to mat lab and I am trying to get to grips with integrating under a curve.
I wanted to see this difference between using the 'trapz(y)' and 'trapz(x,y)' to find the area under a curve of a Gaussian function what I can not seem to understand is why I am getting two different area values and I am trying to figure which one is more accurate.
dataset = xlsread('Lab 3 Results 11.10.18 (1).xlsx','Sheet3','C6:D515');
x=dataset(:,1);
a1=38.38;
b1=1179;
c1=36.85;
d1=6.3
y=a1*exp(-((x-b1)/c1).^2)-d1;
int1=trapz(x,y)
int2=trapz(y)
So when I run this code I get int1=1738.3 and int2=5.78.4 but when I integrated this function by hand using the trapezium rule my ans came out to be nearer int1 rather that int2 is there anyone that could shed some light on this if possible? I just cant imagen visulay how matlab is using the trapz rule two different ways,
Neither implementation is more accurate, but trapz(y) assumes unit spacing of each data point (e.g., spacing between data points is uniformly x = 1). See trapz documentation.
Since you know the x-coordinates, use trapz(x,y).

Matlab : Intersect point of curves

Say for example I have data which forms the parabolic curve y=x^2, and I want to read off the x value for a given y value. How do I go about doing this in MATLAB?
If it were a straight line, I could just use the equation of the line of best fit to calculate easily, however I can't do this with a curved line. If I can't find a solution, I'll solve for roots
Thanks in advance.
If all data are arrays (not analytical expressions), I usually do that finding minimal absolute error
x=some_array;
[~,ind]=min(abs(x.^2-y0))
Here y0 is a given y value
If your data are represented by a function, you can use fsolve:
function y = myfun(x)
y=x^2-y0
[x,fval] = fsolve(#myfun,x0,options)
For symbolic computations, one can use solve
syms x
solve(x^2 - y0)
Assuming your two curves are just two vectors of data, I would suggest you use Fast and Robust Curve Intersections from the File Exchange. See also these two similar questions: how to find intersection points when lines are created from an array and Finding where plots may cross with octave / matlab.

Intersection between curve from points and a line

I'm new in matlab and I'm trying to find the intersection between a curve (from points) and a line.
I've some points and I've plot the interpolation between this points. Now I want to find the intersection between the interpolation (xi,yi) curve and another line.
x = [94.8;84.4;63.1;49.4;40.6;33.8;23.2;20.1;10.2;9.2;7.9];
y = [0; 11.4;29.7;41.6;47.5;50.1;52.9;50.6;32.2;28.1;0];
xi=94.8:-0.1:7.9;
yi=interp1(x,y,xi,'spline');
plot(x,y,'*');
hold on
plot(xi,yi);
I've researched but everything I've found needs a function. I already tried to convert the curve to a function using polyfit but the fit is not good enought.
It is posible to do this in matlab?
Thanks.
Basically, the error message ask you to input a function handle (similar to function pointer in other languages). It's not necessary to convert it into something that matches a mathematical definition of a function (e.g. polynom):
f=#(xi)(interp1(x,y,xi,'spline'))
This can be evaluated at every xi.
Usage like every other function:
f(1)
f(1:3)

Why does my MATLAB function not agree with my calculator?

I am trying to make a function for MATLAB and I just don't see how to set it up right. I set up the function and then I do it manually with a calculator and I seem to get a different answer. I am sure it is the way I set it up. If someone can point out the problem I would greatly appreciate it.
I am trying to make the following function:
x*e^(cos(x))
what I wrote in MATLAB:
function y = fun2(a)
y = a*exp(cos(a))
end
Your code is just fine. The most likely explanation is that your calculator is in deg mode and so treats the argument to cos as an angle measured in degrees. Computer code like MATLAB code always use radians rather than degrees. Switch your calculator to rad mode and the two evaluations will agree.