MATLAB Answers

compacting a large number of plot lines

34 views (last 30 days)
func0=@(x) 5/3*cosd(0)-5/2*cosd(x)+11/6-cosd(40-x);
[phi0 valf]=fzero(func0,0);
func20=@(x) 5/3*cosd(20)-5/2*cosd(x)+11/6-cosd(40-x);
[phi20 valf]=fzero(func20,20);
func40=@(x) 5/3*cosd(40)-5/2*cosd(x)+11/6-cosd(40-x);
[phi40 valf]=fzero(func40,40);
func60=@(x) 5/3*cosd(60)-5/2*cosd(x)+11/6-cosd(40-x);
[phi60 valf]=fzero(func60,60);
func80=@(x) 5/3*cosd(80)-5/2*cosd(x)+11/6-cosd(40-x);
[phi80 valf]=fzero(func80,80);
func100=@(x) 5/3*cosd(100)-5/2*cosd(x)+11/6-cosd(40-x);
[phi100 valf]=fzero(func40,100);
plot([0 20 40 60 80 100],[phi0 phi20 phi40 phi60 phi80 phi100]);grid on;
all functioning ,but how can i do this from 0 to 360 degrees without having to write a large number of lines ?

  0 Comments

Sign in to comment.

Accepted Answer

Subhadeep Koley
Subhadeep Koley on 16 Dec 2020
ind = 1;
angleValues = 0:20:360; % Modify angle values as per your requirement
phiValues = zeros(1, numel(angleValues));
for idx = angleValues
func = @(x) 5/3*cosd(idx)-5/2*cosd(x)+11/6-cosd(40-x);
phiValues(ind) = fzero(func, idx);
ind = ind+1;
end
figure
plot(angleValues, phiValues)
grid on

  3 Comments

Opariuc Andrei
Opariuc Andrei on 16 Dec 2020
yeah ,turns out i introduced some error in what i showed ,in func 100 and the second cosd() is not always cosd(40-x) it actually scales/grows from 0 ,like in the image below with a 10 by 10 degree step cosd(0-x)/ cosd(10-x) and so on.
and my plot looks like this
and what attempted with what you provided is
ind =1;
angleValues = 0:1:360;
phiValues = zeros(1, numel(angleValues));
for idx = angleValues
func = @(x) 5/3*cosd(idx)-5/2*cosd(x)+11/6-cosd(idx-x);
phiValues(ind) = fzero(func, ind);
ind = ind+1;
end
and i get an error that says
" Exiting fzero: aborting search for an interval containing a sign change
because NaN or Inf function value encountered during search.
(Function value at -Inf is NaN.)
Check function or try again with a different starting value.
Exiting fzero: aborting search for an interval containing a sign change
because NaN or Inf function value encountered during search.
(Function value at -Inf is NaN.)
Check function or try again with a different starting value."
ind =1;
angleValues = 0:1:360;
phiValues = zeros(1, numel(angleValues));
for idx = angleValues
if ind > 360
func = @(x) 5/3*cosd(idx)-5/2*cosd(x)+11/6-cosd(idx-x);
phiValues(ind) = fzero(func, ind);
ind = ind+1;
end
end
i tried adding an if in the for loop, i got rid of the inf error but my plot is a flat line .
Sorry for my mistake in the original post .
Subhadeep Koley
Subhadeep Koley on 17 Dec 2020
@Opariuc Andrei You have to pass idx to fzero, not ind. The variable ind is to access phiValues.
Use this code snippet.
ind = 1;
angleValues = 0:10:360;
phiValues = zeros(1, numel(angleValues));
for idx = angleValues
func = @(x) 5/3*cosd(idx)-5/2*cosd(x) + 11/6-cosd(idx-x);
phiValues(ind) = fzero(func, idx);
ind = ind+1;
end
figure
plot(angleValues, phiValues)
grid on
axis tight
Opariuc Andrei
Opariuc Andrei on 17 Dec 2020
tried that too after i posted here second time ,it's just that the graph seemed a little fishy to me when i tried a 1 degree step .

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!