How to graph using a for loop

1 Ansicht (letzte 30 Tage)
Jose Moreno
Jose Moreno am 24 Mär. 2020
Kommentiert: Jose Moreno am 24 Mär. 2020
I am asking for help in plotting a line graph in my "for" loop program. The program is able to run the calculations but I am not able to graph. Below is the code i have created.
clc
f=196.2;
fi=0; finc= pi/180; ff=pi/4
for x=0:pi/180:pi/4;
ac= (f*cos(x))/((sin(pi/6)*cos(x))+(cos(pi/6)*sin(x)))
fprintf('Force of AC is %7.2f\n',ac)
bc= ((ac*cos(pi/6))/cos(x))
fprintf('Force of BC is %7.2f\n',bc)
end
hold on;
xx=x(1:length(x));
plot(xx,ac)

Akzeptierte Antwort

KSSV
KSSV am 24 Mär. 2020
YOu need not use a loop as the other user has suggested you. If you use a loop, you need to intiilaize the array and save it in a loop as below:
f=196.2;
fi=0; finc= pi/180; ff=pi/4 ;
x=0:pi/180:pi/4;
ac = zeros(size(x)) ;
for i = 1:length(x)
ac(i)= (f*cos(x(i)))/((sin(pi/6)*cos(x(i)))+(cos(pi/6)*sin(x(i))))
fprintf('Force of AC is %7.2f\n',ac)
bc= ((ac*cos(pi/6))/cos(x(i)))
fprintf('Force of BC is %7.2f\n',bc)
end
plot(x,ac)

Weitere Antworten (1)

Sriram Tadavarty
Sriram Tadavarty am 24 Mär. 2020
Hi Jose,
This code need not require a for loop. The following can be done to get the same result:
clc
f=196.2;
fi=0; finc= pi/180; ff=pi/4
x=0:pi/180:pi/4;
ac= (f*cos(x))./((sin(pi/6)*cos(x))+(cos(pi/6)*sin(x)))
fprintf('Force of AC is %7.2f\n',ac)
bc= ((ac*cos(pi/6))./cos(x))
fprintf('Force of BC is %7.2f\n',bc)
hold on;
plot(x,ac)
Hope this helps.
Regards,
Sriram
  1 Kommentar
Jose Moreno
Jose Moreno am 24 Mär. 2020
Thanks a lot this has helped by showing me another way.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by