Hello,
I am pretty new to matlab but have to use it for a class. I am currently trying to calculate the maximum force in a member using a moment equation for a range of angles.
I have created a for loop to run through my range of angles to calculate the component of the force in the y direction. When alpha (angle) is 0 the component will just be 1 instead of multiplied by the cosine of the angle because it will be entirely in the y direction.
I created a table to display the alpha angle and what the force should be muitiplied by to find the y component. So it has two columns and a bunch of rows.
I now need to be able to calculate the Force variable out of a moment equation using these values that I calculated. I wrote a program like this alreday that only calculates the component and force for one alpha angle instead of many using this:
ME = ((W*dW)-(FDC*cosd(myalpha)*dCB))==0;
FDC = round((solve(ME,FDC)),3);
Where W, dW, dCB, and myalpha are all known values.
Now I am trying to do that same thing but for a range. This is what my loop looks like:
for alpha = -43.43:1:43.43
if alpha == 0
FDCm = 1;
elseif alpha > 0
FDCm = cosd(alpha);
elseif alpha < 0
FDCm = cosd(alpha);
end
T = [T; alpha FDCm];
end
T
I need to do what I did for one angle for many angles and have it output into a table as well. I have tried many different things and keep getting errors no matter what I try. Any help is appreciated!

5 Kommentare

Whenever you mention an error in the forum, post a copy of the error message.
I do not understand, what you are asking for. The loop can be simplified to:
alpha = (-43.43:43:43).';
T = [alpha, cosd(alpha)];
There is no need for the IF branchs.
I tried doing that but it didn't give me what I want. I am sorry I am trying to figure out how to explain what I need to someone not in my class.
This is my entire code:
clc
lBG = 13.5;
dG = 1.75;
lBH = 34;
dH = 3;
alpha = -43.43:1:43.43;
dCB = 5;
lBE = 32;
W = 450;
dW = 16;
lGH = sqrt((((lBG-dH).^2)+((lBH-dG).^2)-(2*(lBG-dH)*(lBH-dG)*cosd(90+alpha))));
d = alpha;
plot(lGH,d);
lGHmin = min(lGH);
lGHmax = max(lGH);
fprintf('Fully extended hydraulic cylinder:%.2f\n ',lGHmax);
fprintf('Fully retracted hydraulic cylinder:%.2f\n ',lGHmin);
syms W dW FDC dCB Ey Ez By Bz FGH FDCm ME
T = [];
for alpha = -43.43:1:43.43
if alpha == 0
FDCm = 1;
elseif alpha > 0
FDCm = cosd(alpha);
elseif alpha < 0
FDCm = cosd(alpha);
end
T = [T; alpha FDCm];
end
And this is what it outputs:
Fully extended hydraulic cylinder:40.11
Fully retracted hydraulic cylinder:26.17
T =
-43.4300 0.7262
-42.4300 0.7381
-41.4300 0.7498
-40.4300 0.7612
-39.4300 0.7724
-38.4300 0.7834
-37.4300 0.7941
-36.4300 0.8046
-35.4300 0.8148
-34.4300 0.8248
-33.4300 0.8346
-32.4300 0.8440
-31.4300 0.8533
-30.4300 0.8622
-29.4300 0.8710
-28.4300 0.8794
-27.4300 0.8876
-26.4300 0.8955
-25.4300 0.9031
-24.4300 0.9105
-23.4300 0.9175
-22.4300 0.9243
-21.4300 0.9309
-20.4300 0.9371
-19.4300 0.9430
-18.4300 0.9487
-17.4300 0.9541
-16.4300 0.9592
-15.4300 0.9640
-14.4300 0.9685
-13.4300 0.9727
-12.4300 0.9766
-11.4300 0.9802
-10.4300 0.9835
-9.4300 0.9865
-8.4300 0.9892
-7.4300 0.9916
-6.4300 0.9937
-5.4300 0.9955
-4.4300 0.9970
-3.4300 0.9982
-2.4300 0.9991
-1.4300 0.9997
-0.4300 1.0000
0.5700 1.0000
1.5700 0.9996
2.5700 0.9990
3.5700 0.9981
4.5700 0.9968
5.5700 0.9953
6.5700 0.9934
7.5700 0.9913
8.5700 0.9888
9.5700 0.9861
10.5700 0.9830
11.5700 0.9797
12.5700 0.9760
13.5700 0.9721
14.5700 0.9678
15.5700 0.9633
16.5700 0.9585
17.5700 0.9533
18.5700 0.9479
19.5700 0.9422
20.5700 0.9362
21.5700 0.9300
22.5700 0.9234
23.5700 0.9166
24.5700 0.9095
25.5700 0.9021
26.5700 0.8944
27.5700 0.8864
28.5700 0.8782
29.5700 0.8698
30.5700 0.8610
31.5700 0.8520
32.5700 0.8427
33.5700 0.8332
34.5700 0.8234
35.5700 0.8134
36.5700 0.8031
37.5700 0.7926
38.5700 0.7818
39.5700 0.7708
40.5700 0.7596
41.5700 0.7481
42.5700 0.7365
So I add my moment equation to be able to solve for FCD and my loop becomes:
for alpha = -43.43:1:43.43
if alpha == 0
FDCm = 1;
elseif alpha > 0
FDCm = cosd(alpha);
elseif alpha < 0
FDCm = cosd(alpha);
end
T = [T; alpha FDCm];
ME = ((W*dW)-(FDC*FDCm*dCB))==0;
FDC = solve(ME,FDC);
end
But I get the eorror message:
Operator '*' is not supported for operands of type 'struct'.
Error in trial (line 35)
ME = ((W*dW)-(FDC*FDCm*dCB))==0;
That is the equation I need to use and I need to be able to solve for FCD
I mention it again:
if alpha == 0
FDCm = 1;
elseif alpha > 0
FDCm = cosd(alpha);
elseif alpha < 0
FDCm = cosd(alpha);
end
is exactly the same as:
FDCm = cosd(alpha);
Find out, which of the variables is a struct, e.g. by uisng the debugger and the command whos
Okay thank you, I got that to work realizing I forgot to delete part of it the first time I tried to run it. Now that I have cosd(alpha) for all the angles I need in my range, how can I make a calculation using the new variable?
I tried doing this:
for alpha = -43.43:1:43.43
FDCm = cosd(alpha);
FDC = ((W*dW)/(FDCm*dCB));
end
But I keep getting this:
FDC =
(2251799813685248*W*dW)/(1658341119971255*dCB)
When I actually need FCD for every alpha value in my range.
Is there a way to do that calculation and put it as another column in my table? I can't get it to calculate anything but that one line.
David Hill
David Hill am 22 Mär. 2021
Look at my answer below. You should not use symbolics. No loop is needed.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

David Hill
David Hill am 22 Mär. 2021

0 Stimmen

myalpha=-43.43:1:43.43;
for k=1:length(myalpha)
FDC(k)=round((W.*dW)/cosd(myalpha(k))./dCB,3);%I am assuming that W, dW, and dCB are all the same size or are scalar
end

7 Kommentare

I made that my loop but all I get is
ans =
0.001*round((1125899906842624000*W*dW)/(817645190541711*dCB))
But I need it as a number instead of variables and I need a different answer for every alpha value
No loop is needed. Solve function and symbolics are not needed. Solving for FDC algebraically is straight forward.
lBG = 13.5;
dG = 1.75;
lBH = 34;
dH = 3;
alpha = -43.43:1:43.43;
dCB = 5;
lBE = 32;
W = 450;
dW = 16;
lGH = sqrt((((lBG-dH).^2)+((lBH-dG).^2)-(2*(lBG-dH)*(lBH-dG)*cosd(90+alpha))));
d = alpha;
plot(lGH,d);
lGHmin = min(lGH);
lGHmax = max(lGH);
FDC=round(W*dW./cosd(alpha)/dCB,3);
T=[alpha',cosd(alpha)',FDC'];
Serina Robbins
Serina Robbins am 22 Mär. 2021
Thank you, that worked for me. When I display the table it is inscientific notation so its displaying like -0.0434.. How can I stop this? And I will probably be back with more questions as this is my first step sadly.
help format
Serina Robbins
Serina Robbins am 22 Mär. 2021
Bearbeitet: Serina Robbins am 22 Mär. 2021
That worked. Now I believe this will be my last question.. I need to find the maximum FDC, which I did using max(FDC) but I need to find the alpha that corresponds to that max(FDC) value. How can I do that? There is more than one so I need a way that can output more than one answer.
I tried using the indexOfMaxFDC but it is giving me the max cosd(alpha) instead of alpha.
Thank you for all your help!
[~,idx]=max(FDC);
alpha(idx)
Serina Robbins
Serina Robbins am 22 Mär. 2021
That gives me on value but not the other, is there a way to give me the other?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by