How do you change the look of a graph

1 Comment

The top plot is what my plot is supposed to look like, the bottom is what my plot actually looks like, hoe do I fix this. Here is my code:
distance = [1:1:7];
for x=1:length(distance)
Tension(x)= (w*lc*lp)/(x*sqrt((lp^2)-(x^2)));
end
plot(distance,Tension,'-o')
title('Distance vs. Tension');
xlabel('Distance, (m)');
ylabel('Tension, (N)');
grid on;

Sign in to comment.

 Accepted Answer

I don’t have your constants so I can’t reproduce your exact results. This idea does what you want:
w = 3; % Filling Missing Constants
lc = 5; % Filling Missing Constants
lp = 11; % Filling Missing Constants
distance = [0:1:7];
for x=1:length(distance)-1
Tension(x)= (w*lc*lp)/(x*sqrt((lp^2)-(x^2)));
end
distanceP = [zeros(1,length(distance)-1); distance(2:end)];
TensionP = [Tension; zeros(size(Tension))];
plot(distanceP,TensionP,'-o')
title('Distance vs. Tension');
xlabel('Distance, (m)');
ylabel('Tension, (N)');
grid on;
The ‘distanceP’ and ‘TensionP’ are plot matrices that create the correct x and y coordinates to plot. Their construction is relatively straightforward. Take a look at their structures to understand how they work, specifically how they plot each (x,y) pair. Note that it plots each column of each matrix against the corresponding column of the other, taking advantage of MATLAB using column-major addressing.

4 Comments

Thanks but now I need one more thing(shows you must always read the question.). I have to make the lowest tension value (i.e the tension at length 6m) a red line. I honestly haven't the faintest idea where to start. Your help is appreciated
I honestly didn’t see the red line.
This only requires a minor addition to my code, first to find the index of the minimum tension, then use that to identify the corresponding columns of the plotting matrices and overplot it with a thicker red line in the second plot call. (I don’t have your constants so I can’t accurately reproduce your plot.)
The complete, revised code:
w = 3; % Filling Missing Constants
lc = 5; % Filling Missing Constants
lp = 11; % Filling Missing Constants
distance = [0:1:7];
for x=1:length(distance)-1
Tension(x)= (w*lc*lp)/(x*sqrt((lp^2)-(x^2)));
end
[~,mti] = min(Tension); % Minimum Tension Index
distanceP = [zeros(1,length(distance)-1); distance(2:end)];
TensionP = [Tension; zeros(size(Tension))];
plot(distanceP,TensionP,'-o')
title('Distance vs. Tension');
xlabel('Distance, (m)');
ylabel('Tension, (N)');
grid on;
hold on
plot(distanceP(:,mti), TensionP(:,mti), '-r', 'LineWidth',2)
hold off
Thanks a lot.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!