# how can i use for loop for this script

1 view (last 30 days)
shoaib Shoaib on 6 May 2021
for b=1:1:1200
omega(b)=b;
Omega(b)=omega(b)*2*pi;
alpa=-(roh*omega(b)^2/E)-(roh*omega(b)/k*G);
beta=-(roh*A*omega(b)^2/E*I)-(roh^2*omega(b)^4/E*k*G);
for j=1:4
k_j(b) =(-1)^(j(b)/2)*([sqrt(alpa+(-1)^j(b))*sqrt((alpa)^2+4*beta)/2]);
k1(b)=k_j(b);
k2(b)=k_j(b);
k3(b)=k_j(b);
k4(b)=k_j(b);
shoaib Shoaib on 6 May 2021
no i dont want to find j(1:1200) i have to find the value of k1..k4 from the expression
k_j = (-1).^(j/2) .* sqrt(alpa + (-1).^j) .* sqrt(alpa.^2 + 4*beta)/2;
using for loop j=1:4 as this possible, i dont understand how to write this in code, actually k1(b),k2(b),k3(b),k4(b) is matrix element such as
T1=zeros(4,4);
T1(1,1)=k1(b)^-3;
T1(1,2)=k2(b)^-3;
T1(1,3)=k3(b)^-3;
T1(1,4)=k4(b)^-3;
i get this error
% Error in MuPAD command: DOUBLE cannot convert the input expression into a double array.
If the input expression contains a symbolic variable, use the VPA function instead.

Sambit Supriya Dash on 6 May 2021
As per your given expression in the question,
the assumptions of the constants here taken as 1,
The running code is,
roh = 1; A = 1; E = 1; I = 1; k = 1; G = 1;
for b = 1:1200
omega(b) = b;
Omega(b) = omega(b)*2*pi;
alpa(b) = -(roh*omega(b)^2/E)-(roh*omega(b)/k*G);
beta(b) = -(roh*A*omega(b)^2/E*I)-(roh^2*omega(b)^4/E*k*G);
for j = 1:4
k_j =(-1)^(j/2)*((sqrt(alpa(b)+(-1)^j)*sqrt((alpa(b))^2+4*beta(b))/2));
k1(b)=k_j;
k2(b)=k_j;
k3(b)=k_j;
k4(b)=k_j;
end
end
And the symbolic solution would be,
syms roh A E I k G
for b = 1:1200
omega(b) = b;
Omega(b) = omega(b)*2*pi;
alpa(b) = -(roh*omega(b)^2/E)-(roh*omega(b)/k*G);
beta(b) = -(roh*A*omega(b)^2/E*I)-(roh^2*omega(b)^4/E*k*G);
for j = 1:4
k_j =(-1)^(j/2)*((sqrt(alpa(b)+(-1)^j)*sqrt((alpa(b))^2+4*beta(b))/2));
k1(b)=k_j;
k2(b)=k_j;
k3(b)=k_j;
k4(b)=k_j;
end
end
Hope this helps....

### More Answers (2)

Sambit Supriya Dash on 6 May 2021
k_j(b) =(-1)^(j(b)/2)*([sqrt(alpa+(-1)^j(b))*sqrt((alpa)^2+4*beta)/2]);
Provide the original formula in a text or written manner (not the typed one). Then, I will guide you further.
##### 2 CommentsShowHide 1 older comment
Sambit Supriya Dash on 6 May 2021

Sambit Supriya Dash on 6 May 2021
According to your given document,
As per this formula,
Your parameters in the code should be,
alpha(b) = -((rho*w^2)/E)-((rho*w^2)/(k*G));
beta(b) = -((rho*A*w^2)/(E*I))-(((rho^2)*w^4)/(E*k*G));
k_j(b) = (-1^(j*0.5))*sqrt(((alpha(b)+((-1^j)*(sqrt(((alpha(b))^2)+(4*beta(b)))))))*0.5);
For Symbolic Calculations,
syms rho A E I k G
for b=1:1200
omega = b;
w = omega*2*pi;
alpha = -((rho*w^2)/E)-((rho*w^2)/(k*G));
beta = -((rho*A*w^2)/(E*I))-(((rho^2)*w^4)/(E*k*G));
for j=1:4
k_j(b) = (-1^(j*0.5))*sqrt(((alpha+...
((-1^j)*(sqrt(((alpha)^2)+(4*beta))))))*0.5);
end
k1=k_j;
k2=k_j;
k3=k_j;
k4=k_j;
end
For specific values of the parameters,
rho = 1; A = 1; E = 1; I = 1; k = 1; G = 1;
for b=1:1200
omega = b;
w = omega*2*pi;
alpha = -((rho*w^2)/E)-((rho*w^2)/(k*G));
beta = -((rho*A*w^2)/(E*I))-(((rho^2)*w^4)/(E*k*G));
for j=1:4
k_j(b) = (-1^(j*0.5))*sqrt(((alpha+...
((-1^j)*(sqrt(((alpha)^2)+(4*beta))))))*0.5);
end
k1=k_j;
k2=k_j;
k3=k_j;
k4=k_j;
end
Hope this works good.