Graph Multiple Functions in MATLAB

1 view (last 30 days)
rr=140*(1-(0.02/r)^2)+140*(1-4*(0.02/r)^2+3*(0.02/r)^4)*cosd(2*theta);
As for the equation above, I want to plot a graph for theta=0 degrees, 22.5 degrees, 67.5 degrees, and 90 degrees.
Thus the graph would have multiple lines, and there also needs to a legend to show which line is which.
Please help.

Accepted Answer

Ameer Hamza
Ameer Hamza on 14 Nov 2020
Edited: Ameer Hamza on 14 Nov 2020
This shows an example of how it can be done
r = linspace(0.1, 1, 100);
thetas = [0 22.5 67.5 90];
figure();
axes();
hold on
for i = 1:numel(thetas)
theta = thetas(i);
rr = 140*(1-(0.02./r).^2)+140*(1-4*(0.02./r).^2+3*(0.02./r).^4).*cosd(2*theta);
plot(r, rr);
end
legend_strs = compose('$\\theta=%.1f$', thetas);
legend(legend_strs, 'Interpreter', 'latex', 'Location', 'best', 'FontSize', 16);
I have used element-wise operators (.* ./) in my code. Read about them here: https://www.mathworks.com/help/matlab/matlab_prog/array-vs-matrix-operations.html

More Answers (0)

Categories

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

Community Treasure Hunt

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

Start Hunting!