convert negatives matrix values to positive to then be used in mirroring for a plot on a graph

12 views (last 30 days)
I will start at I am modelling a flea response by an animal moving away from the source (in this case the origin at (0,0)). I have had some difficulty when the x value coordinate of the animal position is negative, and I think this is due to the gradient between the source and the position of the animal, it works perfectly fine when the coordinates are positive or when we have a positive x value and negative y value as the graph produced is a line graph moving directly away from the origin, whereas when the x value is negative it goes towards the origin then moves away. How ive done this model is to randomly generate 5 different positions/ coordinates as a matrix and separate the x and y values. My thought process next was to convert any negative x value coordinate to positive, produce the graph and then mirror the lines that had a negative x value earlier through the y axis and this should hopefully produce the graph i want showcasing a flea response from the animal moving away from the source.
The questions sets in, how do I convert the selected negative x values from the matrix named x1, produce the graph, and then mirror those lines only that had been converted. I have attached my code so far below including how i have calculated the new positions (as this is something i need) and hope someone can help with this.
x0 = 0;
y0 = 0;
Sr = [x0 y0]; %Source coordinates
SL = 220; %Source Level dB re 1uPa @ 1m (made up need to find example)
a = 0.25; %Attenuation (made up need to find example)
IL = 180; %Impact Level (made up need to find example)
v = 2; %harbor porpoises swim between 0.5 - 4.2 m/s
t = 5; %seconds
s = v*t; %metres
z1 = [randi([-10 10],1,5); randi([-10 10],1,5)];
z1 = z1';
x1 = z1(1:5,1); %this shows the first 5 numbers in column 1
y1 = z1(1:5,2); %this shows the first 5 numbers in column 2
d = sqrt((x1-x0).^2+(y1-y0).^2); %Pythagoras Theorem for distance between the 2 (Source and Reciever) points
m = (y1-y0)./(x1-x0); %Gradient between the points
r1 = d;
z2 = [x1,y1] + diag(s./sqrt(1+m.^2))*[ones(size(x1)),m]; %New coordinates for new positions 5s later at a speed of 1.5m/s
x2 = z2(1:5,1);
y2 = z2(1:5,2);
r2 = d + s;
z3 = [x2,y2] + diag(s./sqrt(1+m.^2))*[ones(size(x1)),m];
x3 = z3(1:5,1);
y3 = z3(1:5,2);
r3 = d + 2*s;
z4 = [x3,y3] + diag(s./sqrt(1+m.^2))*[ones(size(x1)),m];
x4 = z4(1:5,1);
y4 = z4(1:5,2);
r4 = d + 3*s;
z5 = [x4,y4] + diag(s./sqrt(1+m.^2))*[ones(size(x1)),m];
x5 = z5(1:5,1);
y5 = z5(1:5,2);
r5 = d + 4*s;
X1 = [x1 x2 x3 x4 x5];
Y1 = [y1 y2 y3 y4 y5];
X1 = X1';
Y1 = Y1';
figure
hold on;
plot(X1,Y1, '-*')
title('Flea Response')
xlabel('distance (m)')
ylabel('distance (m)')
  2 Comments
Jon
Jon on 7 Jul 2022
It is difficult from your description to understand what you are exactly trying to achieve. From what I can understand, I think that it is likely that your general approach needs to be improved, rather than finding a workaround by "mirroring" negative values.
Please provide a simple explanation of what it is you are trying to model, and a mathematical description of the flee response that you are trying to model.
One other clarification, I think you mean flee response, not flea response. A flea is a tiny jumping insect that bites cats and dogs, probably not what you are trying to model.
Adil Saeed
Adil Saeed on 7 Jul 2022
yes you are right by saying its a flee response rather than a flea... that said what i am trying to do is build a model for a flee response from the source (being at the origin at (0,0), being a sound source). I have randomly generated 5 different coordinates in a matrix and from this calculated a new position at each coordinate based on speed and time, parameters i have pre determined. The gradient of each line is determined using pythagorases theorem from the original random coordinate and the source. As this is a flee response the gradient will remain the same for each respective line and I am trying to find 5 positions (well technically when there is no impact but I have not shown that detail) on this line.
The error now I have run into is, when i generate the model the lines do not show a direct flee effect, i know this is due to the gradient, as the direction is not defined, and now i am trying to solve this by doing a mirror effect of each of the lines that contained the original negative.
I hope this helps

Sign in to comment.

Accepted Answer

Kevin Holly
Kevin Holly on 7 Jul 2022
x0 = 0;
y0 = 0;
Sr = [x0 y0]; %Source coordinates
SL = 220; %Source Level dB re 1uPa @ 1m (made up need to find example)
a = 0.25; %Attenuation (made up need to find example)
IL = 180; %Impact Level (made up need to find example)
v = 2; %harbor porpoises swim between 0.5 - 4.2 m/s
t = 5; %seconds
s = v*t; %metres
z1 = [randi([-10 10],1,5); randi([-10 10],1,5)];
z1 = z1';
x1 = z1(1:5,1); %this shows the first 5 numbers in column 1
y1 = z1(1:5,2); %this shows the first 5 numbers in column 2
negative_values = x1<0;
x1(negative_values) = -1*x1(negative_values)
x1 = 5×1
3 9 1 2 5
d = sqrt((x1-x0).^2+(y1-y0).^2); %Pythagoras Theorem for distance between the 2 (Source and Reciever) points
m = (y1-y0)./(x1-x0); %Gradient between the points
r1 = d;
z2 = [x1,y1] + diag(s./sqrt(1+m.^2))*[ones(size(x1)),m]; %New coordinates for new positions 5s later at a speed of 1.5m/s
x2 = z2(1:5,1);
y2 = z2(1:5,2);
r2 = d + s;
z3 = [x2,y2] + diag(s./sqrt(1+m.^2))*[ones(size(x1)),m];
x3 = z3(1:5,1);
y3 = z3(1:5,2);
r3 = d + 2*s;
z4 = [x3,y3] + diag(s./sqrt(1+m.^2))*[ones(size(x1)),m];
x4 = z4(1:5,1);
y4 = z4(1:5,2);
r4 = d + 3*s;
z5 = [x4,y4] + diag(s./sqrt(1+m.^2))*[ones(size(x1)),m];
x5 = z5(1:5,1);
y5 = z5(1:5,2);
r5 = d + 4*s;
X1 = [x1 x2 x3 x4 x5];
Y1 = [y1 y2 y3 y4 y5];
X1 = X1';
Y1 = Y1';
figure
hold on;
plot(X1,Y1, '-*')
title('Flea Response')
xlabel('distance (m)')
ylabel('distance (m)')
Mirroring entire line if x1 is negative:
X1(:,negative_values) = -1*X1(:,negative_values);
figure
hold on;
plot(X1,Y1, '-*')
title('Flea Response')
xlabel('distance (m)')
ylabel('distance (m)')
  4 Comments
Adil Saeed
Adil Saeed on 7 Jul 2022
Edited: Adil Saeed on 7 Jul 2022
Apologies, i see it now, was hard to read the graph.
One last thing, is it possible to split the plot into 4 quadrants? so the origin is in the middle of the graph?
Kevin Holly
Kevin Holly on 7 Jul 2022
x0 = 0;
y0 = 0;
Sr = [x0 y0]; %Source coordinates
SL = 220; %Source Level dB re 1uPa @ 1m (made up need to find example)
a = 0.25; %Attenuation (made up need to find example)
IL = 180; %Impact Level (made up need to find example)
v = 2; %harbor porpoises swim between 0.5 - 4.2 m/s
t = 5; %seconds
s = v*t; %metres
z1 = [randi([-10 10],1,5); randi([-10 10],1,5)];
z1 = z1';
x1 = z1(1:5,1); %this shows the first 5 numbers in column 1
y1 = z1(1:5,2); %this shows the first 5 numbers in column 2
negative_values = x1<0;
x1(negative_values) = -1*x1(negative_values);
d = sqrt((x1-x0).^2+(y1-y0).^2); %Pythagoras Theorem for distance between the 2 (Source and Reciever) points
m = (y1-y0)./(x1-x0); %Gradient between the points
r1 = d;
z2 = [x1,y1] + diag(s./sqrt(1+m.^2))*[ones(size(x1)),m]; %New coordinates for new positions 5s later at a speed of 1.5m/s
x2 = z2(1:5,1);
y2 = z2(1:5,2);
r2 = d + s;
z3 = [x2,y2] + diag(s./sqrt(1+m.^2))*[ones(size(x1)),m];
x3 = z3(1:5,1);
y3 = z3(1:5,2);
r3 = d + 2*s;
z4 = [x3,y3] + diag(s./sqrt(1+m.^2))*[ones(size(x1)),m];
x4 = z4(1:5,1);
y4 = z4(1:5,2);
r4 = d + 3*s;
z5 = [x4,y4] + diag(s./sqrt(1+m.^2))*[ones(size(x1)),m];
x5 = z5(1:5,1);
y5 = z5(1:5,2);
r5 = d + 4*s;
X1 = [x1 x2 x3 x4 x5];
Y1 = [y1 y2 y3 y4 y5];
X1 = X1';
Y1 = Y1';
figure
hold on;
plot(X1,Y1, '-*')
title('Flea Response')
xlabel('distance (m)')
ylabel('distance (m)')
Mirroring entire line if x1 is negative:
X1(:,negative_values) = -1*X1(:,negative_values);
figure
hold on;
plot(X1,Y1, '-*')
title('Flea Response')
xlabel('distance (m)')
ylabel('distance (m)')
Change Axes location to origin:
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!