Using different line style to generate plot

I want to use different line style to gengerate the plot, since there are three values overlap in the graph. I don't know how to modify the code, can somebody help me? Thanks.

4 commentaires

Divyam
Divyam le 7 Juin 2023
Hi, please provide the code instead of an image.
Matt J
Matt J le 7 Juin 2023
can somebody help me?
Yes, but first:
(1) Post your code as formatted text so we can copy-paste it.
(2) Attach N and err in a .mat file so that we can plot the data ourselves and demonstrate solutions.
Dan
Dan le 7 Juin 2023
Modifié(e) : Matt J le 7 Juin 2023
Sorry for the inconvenience. Here is the code.
N = 10:20:500;
err = zeros(6,length(N));
for values = 1:length(N)
err(1,values) = abs(calculate_pi("left_end",N(values))- pi);
err(2,values) = abs(calculate_pi("right_end",N(values)) - pi);
err(3,values) = abs(calculate_pi("midpoint",N(values)) - pi);
err(4,values) = abs(calculate_pi("monte_carlo",N(values)) - pi);
err(5,values) = abs(calculate_pi("leibniz",N(values)) - pi);
err(6,values) = abs(calculate_pi("trapezoidal",N(values)) - pi);
end
loglog(N,err','-*')
legend('left-end','right-end','mid-point','monte-carlo','leibniz','trapezodial')
xlabel('N')
ylabel('Error')
Matt J
Matt J le 7 Juin 2023
Modifié(e) : Matt J le 7 Juin 2023
Sorry for the inconvenience. Here is the code.
Yes, but you still haven't attached N and err in a .mat file, as I suggested. Since we don't have calculate_pi(), we cannot regenerate them ourselves.

Connectez-vous pour commenter.

 Réponse acceptée

Matt J
Matt J le 7 Juin 2023
Modifié(e) : Matt J le 7 Juin 2023
N = 10:20:500;
err = rand(6,numel(N)); %fake
h = loglog(N,err');
[h.LineStyle]=deal('-','--','-.',':','--','--');
[h(end-1:end).Marker]=deal('o','x');
legend('left-end','right-end','mid-point','monte-carlo','leibniz','trapezodial',...
location='southwest')
xlabel('N')
ylabel('Error')

Plus de réponses (2)

Chris Burschyk
Chris Burschyk le 7 Juin 2023

0 votes

figure();
hold on
loglog(N,err(1,:)',"-")
loglog(N,err(2,:)',"--")
loglog(N,err(3,:)',"-.")
loglog(N,err(4,:)',":")
loglog(N,err(5,:)',"-^")
loglog(N,err(6,:)',"-v")
hold off
legend('left-end','right-end','mid-point','monte-carlo','leibnitz','trapezodial')
xlabel('N')
ylabel('Error')
Maybe something like this?
Vinayak Agrawal
Vinayak Agrawal le 7 Juin 2023

0 votes

Hi Dan,
According to me this should be the possible updates in your code-
Use different line styles for each dataset in your plot, you can modify the `loglog` function call by specifying the 'LineStyle' parameter. Here is an updated version of your code with different line styles:
N = 10:20:500;
err = zeros(6,length(N));
for values = 1:length(N)
err(1, values) = abs(calculate_pi('left_end', N(values)) - pi);
err(2, values) = abs(calculate_pi('right_end', N(values)) - pi);
err(3, values) = abs(calculate_pi('midpoint', N(values)) - pi);
err(4, values) = abs(calculate_pi('monte_carlo', N(values)) - pi);
err(5, values) = abs(calculate_pi('leibniz', N(values)) - pi);
err(6, values) = abs(calculate_pi('trapezoidal', N(values)) - pi);
end
% Specify line styles
line_styles = {'-', '--', '-.', ':', '-', '-.'};
loglog(N, err', '*', 'LineWidth', 1.5, 'Markersize', 8, 'LineStyle', line_styles);
xlabel('N');
ylabel('Error');
title('Approximation Error vs. N for Different Pi Calculation Methods');
legend('left-end', 'right-end', 'mid-point', 'monte-carlo', 'leibniz', 'trapezoidal');
Here, the `loglog` function call is modified with the addition of the `'LineStyle'` parameter, which is set to a cell array of line styles. The order of the line styles must correspond to the order of the datasets in the matrix `err`. The resulting plot will have a different line style for each dataset.
Hope it helps!

Catégories

En savoir plus sur Mathematics dans Centre d'aide et File Exchange

Produits

Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by