How can I graph multiple x functions to the same y function without repeating the y function each time?

11 views (last 30 days)
I am trying to plot multiple x functions to the same y step. I am creating a graph to model fireworks, and so I want each of my firework particles (modeled as a polynomial projectile motion equation with slight changes to initial velocity each time) to plot to the same y function, without having to type plot(y function, x1 function, y function, x2 function, y function, x3 function, etc.). Is there any simpler way to just plot each x function to that same y function? I use the following function, with my two inputs being the coefficient of the x^2 term, and then a step size.
function [output] = firework_2(input, time_step)
output = (-1*input * (time_step.^2)) + 70
Then, I plot this function vs time_step in my main code, as follows:
time_step = -2:.1:2
figure(3)
plot(time_step, firework_2(2, -2:.1:2))
axis([-20, 20, 0, 90])
I want to be able to plot many different forms of firework_2, to the same time_step, without having to repeat time step each time. As of right now, my code would have to look like this:
plot(time_step, firework_2(2, -2:.1:2), time_step, firework_2(4, -2:.1:2), time_step, firework_2(6, -2:.1:2))
Which is going to take up a lot of time and a lot of space. Is there any way to make each of these firework_2 functions plot to time_step, without rewriting time_step each time?
  2 Comments
Ran Yang
Ran Yang on 13 Apr 2023
Edited: Ran Yang on 13 Apr 2023
You should include your code so we can see what you're doing.
In general, if you're doing something over and over again, you should be writing a for loop. The pseudocode would look something like this:
xfuns = cell(N, 1); % allocate array for N projectiles
for i = 1:N
xfuns{i, 1} = trajectory * rand(i); % your trajectory * some random change each time
end
figure
hold on
% sequentially plot each trajectory
for i = 1:N
plot(xfuns(i), y)
end

Sign in to comment.

Answers (1)

Gokul Nath S J
Gokul Nath S J on 17 Apr 2023
Hi Ran Yang,
Based on my understanding, it seems that you want to plot multiple firework particle function on a same plot without calling the plot function specifically. You can simulate such instances with the help of vectors or by using a matrix. You an simply make a matrix out of the different x functions and then plot it against a y-function.
For example if you have three x-functions with each of them having 91 data points and a y-function having just 91 point vector, you can easily concatenate the five x-functions(dimension : 91 x 3) into a matrix and then plot it against the y-function (91 x 1).
y = (1:0.1:10).';
x1 = sin(y)
x2 = sin(2*y)
x = [x1, x2];
plot(x, y)
Further the if you have multiple x functions say x1, x2, x3.. xm, use a for loop to create the x matrix.
with regards,
Gokul Nath S J

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!