How can I set value of a loop to be the contents of an array?
1 view (last 30 days)
Show older comments
Ahmed El Barbary
on 13 Dec 2016
Commented: Ahmed El Barbary
on 13 Dec 2016
Hello
I wanted to solve 4 equations in 4 unknowns with one being a variable input from 0 to 360, so 1st I wrote an m file
function w=position(theta,a)
w=[(100)+ (175*(cos(theta(2))))-(200*(cos(pi*a/180)))-(150*(cos(theta(1))));
(175*(sin(theta(2))))-(200*(sin((pi*a/180))))-(150*(sin(theta(1))))];
end
Then I made a loop
i=1;
options=optimset('display','off');
for th=0:5:360
the(i)=th;
theta34(:,i)=fsolve(@position,[1 1],options,th)*180/pi;
i=i+1;
end
plot(the(1,:),theta34(1,:));
hold;
plot(the(1,:),theta34(2,:));
grid;
and I got theta34 as an array, now I want to use theta34(2,:) as the value for a for loop to solve the other 2 equations, so i wrote another m file
function w=position1(theta,a)
w =[(theta(2))+(175*(sin(pi*a/180)))+(300*(sin(theta(1))));
(175*(sin(pi*a/180)))+(300*(sin(theta(1))))];
end
and made a similar loop
i=1;
options=optimset('display','off');
for th=theta34
the(i)=th;
theta56(:,i)=fsolve(@position1,[1 1],options,th)*180/pi;
i=i+1;
end
plot(the(1,:),theta56(1,:));
hold;
plot(the(1,:),theta56(2,:));
grid;
however I can't figure out how to turn the values of the loop instead of (for ex.) 0:5:360 to theta34(2,:1)
Thanks in advance
0 Comments
Accepted Answer
Geoff Hayes
on 13 Dec 2016
Ahmed - if you want to iterate over the elements in the second row of theta34, then you should be able to do something like
for k=1:length(theta34(2,:))
th = tehta34(2,k);
% etc.
end
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements 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!