MATLAB Answers

Plotting multiple numbers in 1 graph

1 view (last 30 days)
WARRIOR24
WARRIOR24 on 9 May 2021
Commented: Rena Berman on 29 Jun 2021
I am trying to plot all the values of N for on one graph and it is giving me an error it works for either N = 5, N=9, N=20 by themselves but not all together. As well as i want to change their line weight and their colors and the points to be all different.
N = 5 will be RED and * points
N = 9 will be BLUE and Triangle points
N = 20 will be Green and Circle points
w = .5; %% 2w=1
x = -.5:0.1:.5; %% X (-w,w)
N = [5, 9, 20]; %% N [5,9,20]
delta = 2*w./N;
% Matrix for W
Mat_w = zeros(N,1);
for i = 1:N
Mat_w(i) =(-w+delta.*(i-.5));
end
% U Matrix
u = ones(N,1).*(x.^2);
% F Matrix
f = ones(N,1);
% S Matrix
s = ones(N,N);
for m = 1:N
for n = 1:N
s(m,n) = delta/(2*pi)*(1-log(delta)-1/2*log(abs((m-n).^2-.25))...
-(m-n)*log(abs(m-n+.5)/abs(m-n-.5)));
end
end
u = s\f;
title('Case 2')
hold on
plot(Mat_w, u)
xlim([-w,w])
legend('N=5', 'N=9', 'N=20');
This is the error in the Command line
Error using zeros
Size inputs must be scalar.
Error in HW9_new (line 7)
Mat_w = zeros(N,1);
  2 Comments
Rena Berman
Rena Berman on 29 Jun 2021
(Answers Dev) Restored edit

Sign in to comment.

Answers (1)

VBBV
VBBV on 9 May 2021
w = .5; %% 2w=1
x = -.5:0.1:.5; %% X (-w,w)
N = [5, 9, 20]; %% N [5,9,20]
delta = 2*w./N
delta = 1×3
0.2000 0.1111 0.0500
% Matrix for W
Mat_w = zeros(length(N),1);
for i = 1:length(N)
Mat_w(i) =(-w+delta(i)*(i-0.5));
end
% U Matrix
u = ones(length(N),1).*(x.^2);
% F Matrix
f = ones(length(N),1);
% S Matrix
s = ones(length(N),length(N));
for m = 1:length(N)
for n = 1:length(N)
s(m,n) = delta(m)/(2*pi)*(1-log(delta(m))-1/2*log(abs((m-n).^2-.25))...
-(m-n)*log(abs(m-n+.5)/abs(m-n-.5)));
end
end
u = s\f;
title('Case 2')
hold on
plot(Mat_w, u)
xlim([-w,w])
legend('N=5', 'N=9', 'N=20');
Warning: Ignoring extra legend entries.
  1 Comment
WARRIOR24
WARRIOR24 on 9 May 2021
I think it is wrong, when you plot just N=5 it showes a U shape. If you plot N = 20, it also shows a U but with more points, I need all 3 N values to show up in one graph.

Sign in to comment.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by