Trying to correct this functions output.

2 Ansichten (letzte 30 Tage)
Scott
Scott am 3 Mai 2016
Bearbeitet: Scott am 4 Mai 2016
The assignment asks to write a function to find unique isosceles triangles of integer sides and area. A=sqrt(m*(m-a)(m-b)(m-a) where m = (a+a+b)/2 or P/2. for example P=16 produces a=5, b=6 A=12
clear;
for P = 1:100
[a,b,A] = assfunct1(P);
fprintf('P: %d, a: %d, b: %d, A: %d\n', P, a, b, A);
end;
.
function [a,b,A] = assfunct1(P)
if P<0 || fix(P) ~=P;
disp('error, input is not a non negative integer')
return
end
n = 0 % variable to sum loop passes
m=P/2;
for ii=1:P;
a=ii/2;
b=P-(2*a);
if b>= 2*a || b<=0;
continue
end
for A=sqrt((m*(m-a)*(m-b)*(m-a)));
if fix(A)== A && A ~= 0 && fix(a)==a && fix(b) == b && n == 0
a=a;
b=b;
A=A;
n = n+1;
else fix(A) ~= A || fix(a) ~= a || fix (b) ~= b || A == 0 || n == 1;
a = 0;
b = 0;
A = 0;
continue
end
[P,a,b,A]
end
end
end
  • So we are talking about unique Heronian isosceles triangles
  • My code doesn't output what I want, the [P,a,b,A] gives me Heronian isosceles results but the function output itself is different.
  • As can be seen my [P,a,b,A] call results is different to the fprintf results. The current program doesn't find any results to fprintf.
  • I don't know how to isolate the unique triangles (where 1 heronian isosceles exists for given P) My code if working only reduces the output to 1 not per given P not find unique.
OUTPUT as stands
P: 101, a: 5.050000e+01, b: 0, A: 0
P: 102, a: 51, b: 0, A: 0
P: 103, a: 5.150000e+01, b: 0, A: 0
P: 104, a: 52, b: 0, A: 0
P: 105, a: 5.250000e+01, b: 0, A: 0
P: 106, a: 53, b: 0, A: 0
P: 107, a: 5.350000e+01, b: 0, A: 0
ans =
108 30 48 432 <- from [P,a,b,A] call
P: 108, a: 54, b: 0, A: 0
P: 109, a: 5.450000e+01, b: 0, A: 0
P: 110, a: 55, b: 0, A: 0
P: 111, a: 5.550000e+01, b: 0, A: 0
ans =
112 35 42 588
P: 112, a: 56, b: 0, A: 0
P: 113, a: 5.650000e+01, b: 0, A: 0
P: 114, a: 57, b: 0, A: 0
P: 115, a: 5.750000e+01, b: 0, A: 0
P: 116, a: 58, b: 0, A: 0
P: 117, a: 5.850000e+01, b: 0, A: 0
P: 118, a: 59, b: 0, A: 0
P: 119, a: 5.950000e+01, b: 0, A: 0
P: 120, a: 60, b: 0, A: 0
P: 121, a: 6.050000e+01, b: 0, A: 0
P: 122, a: 61, b: 0, A: 0
P: 123, a: 6.150000e+01, b: 0, A: 0
P: 124, a: 62, b: 0, A: 0
P: 125, a: 6.250000e+01, b: 0, A: 0
ans =
126 35 56 588
P: 126, a: 63, b: 0, A: 0
P: 127, a: 6.350000e+01, b: 0, A: 0
ans =
128 34 60 480
P: 128, a: 64, b: 0, A: 0
P: 129, a: 6.450000e+01, b: 0, A: 0
P: 130, a: 65, b: 0, A: 0
P: 131, a: 6.550000e+01, b: 0, A: 0
P: 132, a: 66, b: 0, A: 0
P: 133, a: 6.650000e+01, b: 0, A: 0
P: 134, a: 67, b: 0, A: 0
P: 135, a: 6.750000e+01, b: 0, A: 0
P: 136, a: 68, b: 0, A: 0
P: 137, a: 6.850000e+01, b: 0, A: 0
P: 138, a: 69, b: 0, A: 0
P: 139, a: 6.950000e+01, b: 0, A: 0
P: 140, a: 70, b: 0, A: 0
P: 141, a: 7.050000e+01, b: 0, A: 0
P: 142, a: 71, b: 0, A: 0
P: 143, a: 7.150000e+01, b: 0, A: 0
ans =
144 37 70 420
P: 144, a: 72, b: 0, A: 0
P: 145, a: 7.250000e+01, b: 0, A: 0
P: 146, a: 73, b: 0, A: 0
P: 147, a: 7.350000e+01, b: 0, A: 0
P: 148, a: 74, b: 0, A: 0
P: 149, a: 7.450000e+01, b: 0, A: 0

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 3 Mai 2016
"You may only use the sqrt() and fix() Matlab functions for this question."
It is not possible to complete the required code under those restrictions. See http://www.mathworks.com/matlabcentral/answers/38787-what-can-be-programmed-without-any-built-in-functions

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by