find distance between 2 points using a function in matlab

Here's a sample function that calculates the distance between two points with (x1, y1) and (x2, y2) coordinates:

main.m
function d = distance(x1, y1, x2, y2)
    d = sqrt((x2-x1)^2 + (y2-y1)^2);
end
79 chars
4 lines

This function takes in four arguments, which are the x and y coordinates of the two points. The Euclidean distance formula is then used to calculate the distance between the two points. The output of the function is the distance value, which can be assigned to a variable or used directly in the code.

Here's an example of how to use this distance function:

main.m
% define two points with (x,y) coordinates
point1 = [1, 2];
point2 = [4, 5];

% call the distance function to calculate the distance between the two points
dist = distance(point1(1), point1(2), point2(1), point2(2));

% display the distance value
disp(dist);
259 chars
10 lines

The expected output of this code is:

4.2426 (rounded to four decimal places)

gistlibby LogSnag