how can i use for loop for this script

2 views (last 30 days)
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);
  5 Comments
DGM
DGM on 6 May 2021
Edited: DGM on 6 May 2021
I see that there are loops. I asked what j(b) is supposed to be. The variable j is a scalar within the scope of the inner loop. You are trying to find j(1:1200). There is no j(2), let alone j(1200). I don't understand what you're trying to do with this expression.
Also: is Omega different than omega, or is that a typo?
This doesn't appear to need any loops. Just use two orthogonal vectors, and your results will be 1200x4
b = (1:1200).';
roh = 0.1;
A = 1;
E = 1;
k = 1;
G = 1;
I = 1;
omega = 2*pi*b; % i'll assume that Omega is a typo
alpa = -(roh*omega.^2/E)-(roh*omega/k*G);
beta = -(roh*A*omega.^2/E*I)-(roh^2*omega.^4/E*k*G);
j=1:4;
% this whole expression makes no sense
%k_j =(-1)^(j(b)/2)*([sqrt(alpa+(-1)^j(b))*sqrt((alpa)^2+4*beta)/2]);
% if we assume j(b) can be replaced with j
k_j = (-1).^(j/2) .* sqrt(alpa + (-1).^j) .* sqrt(alpa.^2 + 4*beta)/2;
% i guess these are just placeholders?
k1=k_j;
k2=k_j;
k3=k_j;
k4=k_j;
shoaib Shoaib
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.

Sign in to comment.

Accepted Answer

Sambit Supriya Dash
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
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.

Sambit Supriya Dash
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.

Categories

Find more on Symbolic Math Toolbox 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!