Clear Filters
Clear Filters

I have a script but i didn't understand the flow of the program could anybody please give me an explanation for this script?

1 view (last 30 days)
%%Plotting the terrain profile
x1 = -35:0.001:3;
x2 = 3.001:0.001:25;
x3 = 25.001:0.001:100;
x4 = 100.001:0.001:160;
% Values of slope for all the slope equations
m1 = 0;
m2 = 0;
m3 = 0.133333;
m4 = -0.02;
% Values of constant for all the slope equations
c1 =15;
c2 =0;
c3 =0;
c4 =10;
% Slope equations
y1 = m1.*(x1)+c1;
y2 = m2.*(x2-10)+c2;
y3 = m3.*(x3-25)+c3;
y4 = m4.*(x4-100)+c4;
% Plotting the terrain profile
x=[x1 x2 x3 x4]';
y=[y1 y2 y3 y4]';
figure(1)
plot(x,y); % Plotting the Ground elevation vs distance
ylim([0 25]);
xlabel('Distance [m]');
ylabel('Ground Elevation');
title('Ground Elevation vs distance (m)');

Accepted Answer

Walter Roberson
Walter Roberson on 18 Nov 2017
For the code
y1 = m1.*(x1)+c1;
y2 = m2.*(x2-10)+c2;
y3 = m3.*(x3-25)+c3;
y4 = m4.*(x4-100)+c4;
the lines after the first are "translating" the standard y = m*x+b to start at x=10, x=25, and x=100 respectively.
Mathematically these adjustments to x could also be folded into the c constants by increasing the c constants by -m2*10, -m3*25, and -m4*100 respectively, like
y2 = m2 .* x2 + (c2-m2*10)
It is just changing the intercept of the line in a way that is easier to think about in this case.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!