Thursday, May 22, 2014

Solving a simple geometric problem with matlab!



Example about simple geometric problem:
Write Matlab code to compute the Areas of 4 Circles and 4 acute (isosceles) Triangles shown in the figure below, knowing that the first circle center point in x-axis = 100, radius and triangle base = 5, Triangle height (h) = 3.5  and both are increasing for in 1.25 (of the previous radius and height), with center point displacement = radius?
 Solution:
We can solve the problem for any n cases, here n = 4, so the Matlab script could be something like this:
%===========================================
%= Computing Areas of Circles and Triangles  That share              =
%= the radius as the Acute (isosceles) triangle's side that both =
%= are  increasing at a fixed ratio for n cases!                                     =
%===========================================
clc
x=100;
r=5;
h=3.5;
n = 4;
% Computing first Circle and Triangle Areas
CircleArea= r^2*pi
TriangleArea=(r * h) / 2
% Computing the rest increasing Areas n-1 = 4 -1 = 3 loops
for i=1:3                                % Or,    for i=1:n-1 (in general)
    r = r*1.25;
    h = h*1.25;
    x = x+r;
    CircleArea = r^2*pi
    TriangleArea = (r * h) / 2
end