how to create a ramp signal when it hits an x value in matlab

To create a ramp signal in MATLAB that hits a specific x value, you can use a combination of conditional statements and linspace function.

Here is an example code that creates a ramp signal with a slope of 2 until it hits the x value of 5, after which the slope changes to 5:

main.m
% Define the x interval
x = linspace(0, 10, 100);

% Initialize the y signal to zeros
y = zeros(size(x));

% Set the slope and the x value where the slope changes
slope1 = 2;
slope2 = 5;
x_switch = 5;

% Generate the ramp signal with the two slopes
for i = 1:length(x)
    if x(i) < x_switch
        y(i) = slope1 * x(i);
    else
        y(i) = slope1 * x_switch + slope2 * (x(i) - x_switch);
    end
end

% Plot the signal
plot(x, y);
437 chars
23 lines

You can modify the slope1, slope2, and x_switch values to match your desired ramp signal.

gistlibby LogSnag