store iterative solutions to matrix

E=input('\nModulus of Elasticity, E(GPa): ');
L=input('Length of each member, L(m): ');
A=input('Area of c/s, A(m^2): ');
Theta=input('Theta(degree): ');
delta_max=input('Maximum Delta(m): ');
a=sind(Theta);
h=L*a;
delta=0:0.01:delta_max;
del=length(delta);
P_L=zeros(1,del);
for i=1:del
P_L(i)=(2*E*A*10^9*a*a* delta(i))/(L*1000);
end
Need to store all iterative values of P_L as a vector
For E = 70, L=5.41, A=0.0006;delta_max=2; theta=33.69;
Last answer = 9.56 * (10^3)

 Risposta accettata

Star Strider
Star Strider il 23 Set 2020

0 voti

Define the range of the independent variables as vectors, then use the ndgrid function to create matrices from them that you can use in the ‘P_L’ assignment (without the indices, since it will be a matrix the same size as the argument matrices) to calculate it from the resulting matrices.
To see the output with respect to each set of variables, use the reshape function on all the argument matrices, and ‘P_L’ to create them each as column vectors. Then use those with the table function to create a table of the results, with appropriate variable names.
There is no way to plot that function against all the independent variables.

4 Commenti

but the below logic is working.why?
a=0:0.1:1;
m=5;
F=zeros(1,length(a));
for i=2:length(a)
F(i)=m*a(i);
end
Star Strider
Star Strider il 23 Set 2020
Modificato: Star Strider il 23 Set 2020
It works because you are only using one variable.
Note that:
F = m*a;
also works, and completely avoids the explicit loop.
To do that same sort of calculation with ‘P_L’, and without any explicit loops, assuming all the matrices were created first (note the element-wise operations using the dot operator):
P_L = (2.*E.*A.*1E9.*a.*a.*delta)./(L.*1000);
or if you want to make ‘P_L’ into an anonymous function:
P_L = @(E,A,a,delta,L) (2.*E.*A.*1E9.*a.*a.*delta)./(L.*1000);
and then call it in an assignment to assign the results to another variable.
EDIT — (23 Sep 2020 at 17:19)
To illustrate the approach:
E = 69:71;
L = 5:0.1:6;
a = (5:7)*1E-4;
delta = 0:0.5:2;
theta = 33:0.2:34 ;
P_Lfcn = @(E,A,a,delta,L) (2.*E.*A.*1E9.*a.*a.*delta)./(L.*1000);
[Em,Lm,am,dlm,thm] = ndgrid(E,L,a,delta,theta);
P_L = P_Lfcn(Em,Lm,am,dlm,thm);
TOut = table(Em(:),Lm(:),am(:),dlm(:),thm(:),P_L(:), 'VariableNames',{'E','L','a','delta','theta','P_L'});
First_5_Rows = TOut(1:5,:)
Last_5_Rows = TOut(end-4:end,:)
with:
First_5_Rows =
5×6 table
E L a delta theta P_L
__ ___ ______ _____ _____ ___
69 5 0.0005 0 33 0
70 5 0.0005 0 33 0
71 5 0.0005 0 33 0
69 5.1 0.0005 0 33 0
70 5.1 0.0005 0 33 0
.
Thank you so much for clarifying my doubt and providing the detailed illustration.
As always, my pleasure!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by