can someone help me create this function
12 views (last 30 days)
Show older comments
toufik mec
on 5 Jan 2020
Commented: Walter Roberson
on 6 Jan 2020
Can some one create matlab code for this function? I want the answers of f from 0 to 100.
m= 0 to 5.
n= 0 to 5.
r= 0 to 5 (whole numbers only)
There is one condition : m and n and r never equal 0 at the same time. If first and second = 0 the third never equal to 0
1 Comment
Walter Roberson
on 6 Jan 2020
Our policy is that we do not close Questions that have a meaningful attempt at an Answer -- not unless the Question was abusive or violated legal constraints.
Accepted Answer
Image Analyst
on 5 Jan 2020
Edited: Image Analyst
on 5 Jan 2020
Did you try the super-obvious brute force method of a triple for loop with an "if" test?
c = 340;
Lx = 7.85;
Ly = 6.25;
h = 4.95;
f = zeros(6, 6, 6);
for m = 0 : 5
for n = 0 : 5
for r = 0 : 5
if m==0 && n==0 && r==0
% Don't allow all 3 to be zero at the same time.
continue;
end
t = sqrt((m/Lx)^2 + (n/Ly)^2 + (r/h)^2);
% Assign t to f if t is in the range 0-100.
if t >= 0 && t <= 100
f(m+1, n+1, r+1) = t;
end
end
end
end
fprintf('Done!\n');
Otherwise you could vectorize it with meshgrid().

2 Comments
Image Analyst
on 5 Jan 2020
As long as it's not homework (because you can't turn in my work pretending it's your own), you can use this code:
c = 340;
Lx = 7.85;
Ly = 6.25;
h = 4.95;
f = zeros(6*6*6, 4);
row = 1;
for m = 0 : 5
for n = 0 : 5
for r = 0 : 5
temp = (c/2) * sqrt((m/Lx)^2 + (n/Ly)^2 + (r/h)^2);
if m+n+r >= 1 && temp <= 100
f(row, :) = [m, n, r, temp];
row = row + 1;
end
end
end
end
% Crop to only however many we used.
f = f(1:row-1, :);
% Sort on column 4
f = sortrows(f, 4);
% Print out all the rows.
fprintf('m n r f(m,n,r)\n-------------\n');
for row = 1 : size(f, 1)
fprintf('%d %d %d %.1f\n', ...
f(row, 1), f(row, 2), f(row, 3), f(row, 4));
end
fprintf('Done!\n');
It will give the table shown in your image.
More Answers (2)
David Hill
on 5 Jan 2020
count=0;
for m=0:5
for n=0:5
for r=0:5
if r+m+n==0
break;
end
count=count+1;
f(count)=170*sqrt(m^2/61.6225 + n^2/39.0625 + r^2/24.5025);
end
end
end
end
David Hill
on 5 Jan 2020
count=0;
for m=0:5
for n=0:5
for r=0:5
if r+m+n==0
continue;
end
t=170*sqrt(m^2/61.6225 + n^2/39.0625 + r^2/24.5025);
if t >= 0 && t <= 100
count=count+1;
f(count,:)=[m,n,r,t];
end
end
end
end
See Also
Categories
Find more on Data Type Identification 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!