1

I am looking to calculate an array from a formula using x and y variables, the domain of x is (0,50) and y is (0,30) . I am asked to discretise the domain of x and y with 0.01 separation between points, then compute L(x,y) (which I have a formula for)(This will be points of a graph, ultimately I'm looking for the min lengths between points)

I'm not sure what I need to define in my script because if I define x and y as arrays with 0.01 separation they end up being uneven and unable to calculate as the arrays are uneven

%change these values for A, B and C positions
Ax=10;
Ay=5;

Bx=15;
By=25;

Cx=40;
Cy=10;


x = 0:0.01:50; % Array of values for x from 0-50 spaced at 0.01
y = 0:0.01:30; % Array of values for y from 0-30 spaced at 0.01


%length of point P from A, B and C and display
Lpa=sqrt((Ax-x).^2+(Ay-y).^2);
Lpb=sqrt((Bx-x).^2+(By-y).^2);
Lpc=sqrt((Cx-x).^2+(Cy-y).^2);

L=Lpa+Lpb+Lpc

I am getting an error telling me the two matrix are not even which makes sense to not work but I'm not sure how to define a matrix that will result in the minimum x and y values I am after.

Any help would be greatly appreciated.

Ryan
  • 15
  • 3

1 Answers1

0

You want to calculate L for each possible pair of x and y. In other words, for the first value of x = 0, you will calculate L for all y values from 0 to 30, then for next value of x = 0.01, you will do the same and so on.

MATLAB has a really cool function called meshgrid to create a matrix for every pair of x and y. So after generating x and y, change your code to the following to get a 2D matrix for L -

[X, Y] = meshgrid(x, y)

%length of point P from A, B and C and display
Lpa = sqrt((Ax - X).^2 + (Ay - Y).^2);
Lpb = sqrt((Bx - X).^2 + (By - Y).^2);
Lpc = sqrt((Cx - X).^2 + (Cy - Y).^2);

L = Lpa + Lpb + Lpc
Abhineet Gupta
  • 624
  • 4
  • 12
  • You guys are the greatest! I got a one line answer from my tutor for this! And she gets paid!! Anyway! So if I am understanding correctly I can set x=linspace (0,50,3001); % Array of values for x from 0-50 y = 0:0.01:30; % Array of values for y from 0-30 spaced at 0.01 and this will calculate correctly? ( I am not infront of matlab right now) – Ryan Jul 28 '19 at 20:13
  • yes, it will if that's what you want. However your question asked for both `x` and `y` to have a spacing of 0.01, and your solution would not satisfy that for `x`. – Abhineet Gupta Jul 29 '19 at 02:28
  • So how would I go about satisfying it for x as well? If spaced evenly it just won't work as the two arrays will be different sized – Ryan Jul 29 '19 at 08:59
  • In the solution I proposed, the matrices are all the same size. – Abhineet Gupta Jul 29 '19 at 16:29
  • after looking at your answer more closely I see that, sorry! Thankyou! – Ryan Jul 29 '19 at 19:49