ploting a 2D graph with multiple lines

Hi, I'm a bigginer and what I need is too specific.
I need to plot a graph that looks exaclty like this:
I have a h vector of size 4. a matrix X of size 4x39 and a matrix f(x) of size 4x39.
each line represents the ploting of row i in matrix X with corresponding row i in matrix f(x) for h(i).
Can someone please tell me how to plot this?
Thank you

 Accepted Answer

Ameer Hamza
Ameer Hamza on 12 Nov 2020
Edited: Ameer Hamza on 12 Nov 2020
Something like this
X; % 4x39
f; % 4x39
h; % 4x1
plot(X.', f.')
xlabel('x')
ylabel('(approximate) f(x)')
legend_strs = compose('Approximate f with h=%.2f', h)
legend(legend_strs)

6 Comments

Ameer, Thank you! this is almost exactly what I need. My graph now looks like this:
The sharp staright lines are there because the X matrix is 4x39 but only the 39th row (represented by the purpule line in the graph) has 39 elements. the other rows are shorted. but in order to define the matrix I had to fill on the empty cells with zeros.
so, if row i in X has 39 values and row j has 39-n values and the n last cells in the row are zeros, how can I not plot all the zeros in my graph?
Thank you
I didn't understand it correctly. Can you share your data in a .mat file?
Yes! thank you.
I have two variables in F_X_data.mat.
U is equivalent to f in your above answer. and X_in is equivalent to X.
row i in X_in is the x axis of line i. and row i in U is the y axis of line i.
as you can see, all of the rows (in both X_in and U) have the same length. But, that is only because I initialized the matrices with zeros.
If row i has non zero values in it's first n cells, then the rest of the row is filled with zeros. and that is what is causing these sharp straight lines to zero in my graph:
I need my graph to not "see" the zeros. and thus, not plot them.
How can I handle this issue?
Thank you
Just replace 0 with nan. They will not be visible in plot()
figure();
ax = axes();
hold(ax);
U(U==0) = nan;
X_in(X_in==0) = nan;
for i = 1:4
plot(X_in(i,:), U(i,:))
end
I am glad to be of help!

Sign in to comment.

More Answers (0)

Categories

Products

Tags

Community Treasure Hunt

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

Start Hunting!