Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

How to run this MATLAB codes in R-Programming?

Asked by Tin Tran on 5 May 2013

Subroutine: function[y,concen]=fungi(n,t) % n- number of fungi % t- number of steps % y- max distance in each step % concen- density of points at given distance m1(1,1:n)=1:n; m2(1,1:n)=(1:n)*0; y(1)=0; for i=1:t alfa=rand(1,n)*pi; d=randn(1,n); m1(i+1,1:n)=m1(i,1:n)+(d.*cos(alfa)); m2(i+1,1:n)=m2(i,1:n)+(d.*sin(alfa)); if any(m2(i+1,:)<0) xx=find(m2(i+1,:)<0); for j=1:length(xx) m2(i+1,xx(j))=-m2(i+1,xx(j)); end; end; y(i+1)=max(max(m2(i+1,;)),y(i)); end; maxy=y(t+1); deltay=maxy/(2*t); pop=n; pointotal=sum(sum(m2(1:t+1,:)<=(maxy))); %pointotal-all points ilosc(1)=n/pointotal; for i=1:2*t total=sum(sum(m2(1:t+1,:)<=((i/(2*t))*maxy))); concen(i+1)=total-pop; concen(i+1)=concen(i+1)/pointotal; pop=total; end; subplot(2,1,1); plot([1,n],[0,0],'k','LineWidth',2); % hold on; for i=1:length(m1(1,:)) plot(m1(:,1),m2(:,i),'-*','Color',[rand(1),rand(1),rand(1)]); % hold on; end; xlabel('X'); ylabel('Y'); subplot(2,1,2); plot([0:deltay:maxy],concern); xlabel('distance'); ylabel('density'); % hold off; pause(0.01);

Driver Statements:

function y=calcul(n,t) for j=1:t for i=:100 [yy,i1]=fungi(n,j); point(i)=yy(j+1); end; y(j)=sum(point)/100; end;

0 Comments

Tin Tran

Products

No products are associated with this question.

1 Answer

Answer by Iman Ansari on 6 May 2013

Hi.

function y=calcul(n,t) 
for j=1:t 
    for i=1:100 
        [yy,i1]=fungi(n,j); 
        point(i)=yy(j+1); 
    end; 
    y(j)=sum(point)/100; 
end;
function [y,concern]=fungi(n,t) 
% n- number of fungi 
% t- number of steps 
% y- max distance in each step 
% concen- density of points at given distance 
m1(1,1:n)=1:n;
m2(1,1:n)=(1:n)*0; 
y(1)=0; 
for i=1:t 
    alfa=rand(1,n)*pi; 
    d=randn(1,n); 
    m1(i+1,1:n)=m1(i,1:n)+(d.*cos(alfa)); 
    m2(i+1,1:n)=m2(i,1:n)+(d.*sin(alfa)); 
    if any(m2(i+1,:)<0) 
        xx=find(m2(i+1,:)<0); 
        for j=1:length(xx) 
            m2(i+1,xx(j))=-m2(i+1,xx(j)); 
        end; 
    end; 
    y(i+1)=max(max(m2(i+1,:)),y(i)); 
end; 
maxy=y(t+1); 
deltay=maxy/(2*t); 
pop=n; 
pointotal=sum(sum(m2(1:t+1,:)<=(maxy))); 
%pointotal-all points 
ilosc(1)=n/pointotal; 
for i=1:2*t 
    total=sum(sum(m2(1:t+1,:)<=((i/(2*t))*maxy))); 
    concern(i+1)=total-pop; 
    concern(i+1)=concern(i+1)/pointotal; 
    pop=total; 
end; 
subplot(2,1,1); 
plot([1,n],[0,0],'k','LineWidth',2); 
% hold on; 
for i=1:length(m1(1,:)) 
    plot(m1(:,1),m2(:,i),'-*','Color',[rand(1),rand(1),rand(1)]); 
    % hold on; 
end; 
xlabel('X'); 
ylabel('Y'); 
subplot(2,1,2); 
plot([0:deltay:maxy],concern); 
xlabel('distance'); 
ylabel('density'); 
% hold off; 
pause(0.01);

0 Comments

Iman Ansari

Contact us