loop calculation and skipping the numbers
1 view (last 30 days)
Show older comments
YOGESHWARI PATEL
on 6 Dec 2021
Commented: Star Strider
on 8 Dec 2021
d=20;
h=2;
g=9.8;
u=zeros(1)
v=zeros(1)
for x=[1 6 11 16 21 ]
for t=[1 6 11 16 21 26 31]
u(x,t)=h*sqrt(g/d)*(sech((sqrt(3*h/4*d^3))*(((x-1)/10)-(sqrt(g/d)*(1+(h/2*d))*((t-1)/10)))))^2;
v(x,t)=h*(sech((sqrt(3*h/4*d^3))*((x-1/10)-(sqrt(g/d)*(1+(h/2*d))*(t-1/10)))))^2;
end
end
disp(u)
disp(v)
I use this code to evaluate the value of u and v at different x and t .I want to skip the values so i wish this code .But all the values are not displayed.
0 Comments
Accepted Answer
Star Strider
on 6 Dec 2021
The indexing is incorrect. It leaves gaps. The gaps can be eliminated with a more efficient indexing approach.
This fixes that problem, however other problems may remain.
d=20;
h=2;
g=9.8;
% u=zeros(1)
% v=zeros(1)
xv=[1 6 11 16 21 ];
tv=[1 6 11 16 21 26 31];
u = NaN(numel(xv), numel(tv));
v = NaN(numel(xv), numel(tv));
for k1 = 1:numel(xv)
x = xv(k1);
for k2 = 1:numel(tv)
t = tv(k2);
u(k1,k2)=h*sqrt(g/d)*(sech((sqrt(3*h/4*d^3))*(((x-1)/10)-(sqrt(g/d)*(1+(h/2*d))*((t-1)/10)))))^2;
v(k1,k2)=h*(sech((sqrt(3*h/4*d^3))*((x-1/10)-(sqrt(g/d)*(1+(h/2*d))*(t-1/10)))))^2;
end
end
disp(u)
disp(v)
.
2 Comments
Star Strider
on 8 Dec 2021
Preallocating the matrices, then referencing the variables as elements of the original variable vectors.
More Answers (0)
See Also
Categories
Find more on Mathematics and Optimization 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!