How to plot the continuous linear graph of y(n) + y(n-1) + ... + y(1)

조회 수: 31 (최근 30일)
Peiman
Peiman 2024년 10월 25일 18:29
답변: Phineas 2024년 10월 28일 9:37

I have a hundred discrete values in Matlab. For example: y1=2 y2=-1 y3=1; I know how to plot each of them as dots. But I would like to have a graph of them, where for x=0 on x axis, y1=y1 itself on y axis, for x=1 on the axis y2=y2+y1, and for x=2 y3=y3+y2+y1, etc. Like this: plot(y(n+1)+y(n)+...+y(1), x(n)) That is all my question above. Imagine on first time (x=0), I have 2 apple, but on the second time(x=1) since I have eaten 1 of them (-1), then I did addition operation and 2+(-1) means 1 remained for me on 2nd time (on x=1) //// X is only 0 1 2 3 numbers on the x axis of the chart. Don't get confused, don't think about it. It's the axis of the plot to draw the y above it. Imagine the addition of y1&y2 data happens on x=1 then on x=2. x1 x2 like t1 t2... The plot is 2 dimensional not 3d. It doesn't have x y z, only x and y. x is static number on time. First time second time etc. //// All my question is simplified here: How to plot the continuous linear graph of y(n) + y(n-1) + ... + y(1)

채택된 답변

Voss
Voss 2024년 10월 25일 18:49
편집: Voss 2024년 10월 25일 18:49
y = [2 -1 1 2 -1 -2 -1 2 3 -2 1];
plot(0:numel(y)-1,cumsum(y),'.-')

추가 답변 (1개)

Phineas
Phineas 2024년 10월 28일 9:37
To plot the continuous linear graph of the cumulative sums of your discrete values in MATLAB, you can follow these steps. Assuming you have your discrete values stored in an array, you can compute the cumulative sum and then plot the results.
Here's a step-by-step guide with a MATLAB example:
  1. Define Your Discrete Values: Store your discrete values in a vector.
  2. Compute the Cumulative Sum: Use MATLAB’s built-in function to calculate the cumulative sum of your vector.
  3. Plot the Results: Use the plot function to create the graph.
Example MATLAB Code:
% Step 1: Define your discrete values
y = [2, -1, 1]; % Example values
% Step 2: Compute the cumulative sum
cumulative_y = cumsum(y);
% Step 3: Define x values for plotting
x = 0:length(y)-1; % x will be [0, 1, 2]
% Step 4: Plot the cumulative sums
figure; % Create a new figure window
plot(x, cumulative_y, '-o'); % Plot with dots at data points
xlabel('Time (x)'); % Label for the x-axis
ylabel('Cumulative Sum of y'); % Label for the y-axis
title('Cumulative Sum Plot'); % Title of the plot
grid on; % Add a grid for better readability
Explanation:
  • y = [2, -1, 1]: This line defines your discrete values.
  • cumulative_y = cumsum(y): This computes the cumulative sum of the vector y, so for your example, it will calculate the values as follows:
  • At x=0: y(1)=2y(1) = 2y(1)=2
  • At x=1: y(1)+y(2)=2+(−1)=1y(1) + y(2) = 2 + (-1) = 1y(1)+y(2)=2+(−1)=1
  • At x=2: y(1)+y(2)+y(3)=2+(−1)+1=2y(1) + y(2) + y(3) = 2 + (-1) + 1 = 2y(1)+y(2)+y(3)=2+(−1)+1=2
  • plot(x, cumulative_y, '-o'): This plots the cumulative sums against x, using dots to indicate the discrete points.
Result:
When you run this code in MATLAB, you will see a plot showing the cumulative sums of your values over the defined x range.
If you need further assistance or more complex visualizations, consider reaching out for MATLAB assignment help.
For expert support:
Don't hesitate to ask for help with your MATLAB assignments!

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by