can someone help me create this function

13 Ansichten (letzte 30 Tage)
toufik mec
toufik mec am 5 Jan. 2020
Kommentiert: Walter Roberson am 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 Kommentar
Walter Roberson
Walter Roberson am 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Image Analyst
Image Analyst am 5 Jan. 2020
Bearbeitet: Image Analyst am 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().
1.JPG
  2 Kommentare
toufik mec
toufik mec am 5 Jan. 2020
the anwser is something like this
Image Analyst
Image Analyst am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

David Hill
David Hill am 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
  1 Kommentar
toufik mec
toufik mec am 5 Jan. 2020
f between 0-100 only how to limit that and if u can give me the answers with table like mentioned above and ty

Melden Sie sich an, um zu kommentieren.


David Hill
David Hill am 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

Kategorien

Mehr zu Data Type Identification finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by