How can I make this a callable function?

8 visualizzazioni (ultimi 30 giorni)
BAILEY MCMASTER
BAILEY MCMASTER il 3 Mag 2023
Risposto: Animesh il 12 Mag 2023
function [i,IS]=firststateupdate(N,P_ub,w1)
for i=1:N
w1=randi([0,1],1);
if IS(i)==2 && w1<P_ub
IS(i)=1;
end
end
end
Not enough input arguments.
Error in firststateupdate (line 2)
for i=1:N
  5 Commenti
BAILEY MCMASTER
BAILEY MCMASTER il 4 Mag 2023
So here is my project I'm trying to make this section of code its own function. I have it defined. How do I make this code its own callable function. The normal code works, but I'm trying to snip things into there own function. Hers my base code.
N=10; % number of integrins
L=100; % size of the domain 100nm
D=.01; % diffusion coefficient
dt=1; % time step
F=randi([1, 10],1); % force on the bond with a random number of 1 to 10
P_a=randi([0,1],1); % binding rate of a random number from 0 to 1
P_ub=1/(1.5*F); % unbinding rate based upon our previous values
Tmax=500; % max time
% Intialize our integrins
x=randi([0,360],N,1); % x-coordinates generated randomily for each integrin
y=randi([0,50],N,1); % y-coordinates generated for each
state=ones(N,1); % Each integrin is set to an inactive state at 0
% our figure
figure;
axis([0 L 0 L]);
set(gca,'nextplot','replacechildren');
% position update
for t = 0:dt:Tmax
% colors for our integrin states
colors = repmat([1 0 0], N,1); % red for inactive state
colors(state == 2,:,:) = repmat([0 1 0],sum(state == 2),1);
% plotting our integrins
scatter(x,y,50,colors,'filled');
title(sprintf('Time=%.2f',t));
drawnow;
for i=1:N
w=randi([0,1],1);
if state(i)==1
dx=randi([-360,360],1);
dy=randi([-50,50],1);
x(i)=x(i)+dx;
y(i)=y(i)+dy;
quiver(x(i),y(i))
end
end
x=mod(x,L);
y=mod(y,L);
for i=1:N
w_1=randi([0,1],1);
if w_1 < P_a && state(i)==1
state(i)=2;
end
end
for i=1:N
w1=randi([0,1],1);
if state(i)==2 && w1<P_ub
state(i)=1;
end
end
Displacement=sqrt((mean(x.^2))/N);
end
Torsten
Torsten il 4 Mag 2023
Modificato: Torsten il 4 Mag 2023
Think about what the function is supposed to do.
This will automatically tell you which inputs are needed and which outputs are to be supplied.

Accedi per commentare.

Risposte (1)

Animesh
Animesh il 12 Mag 2023
Hey BAILEY,
I understand that you are trying to create and call functions inside the script.
I tried implementing this at my end and it is working fine. I am attaching the modified base code below:
N=10; % number of integrins
L=100; % size of the domain 100nm
D=.01; % diffusion coefficient
dt=1; % time step
F=randi([1, 10],1); % force on the bond with a random number of 1 to 10
P_a=randi([0,1],1); % binding rate of a random number from 0 to 1
P_ub=1/(1.5*F); % unbinding rate based upon our previous values
Tmax=500; % max time
% Intialize our integrins
x=randi([0,360],N,1); % x-coordinates generated randomily for each integrin
y=randi([0,50],N,1); % y-coordinates generated for each
state=ones(N,1); % Each integrin is set to an inactive state at 0
% our figure
figure;
axis([0 L 0 L]);
set(gca,'nextplot','replacechildren');
% position update
for t = 0:dt:Tmax
% colors for our integrin states
colors = repmat([1 0 0], N,1); % red for inactive state
colors(state == 2,:,:) = repmat([0 1 0],sum(state == 2),1);
% plotting our integrins
scatter(x,y,50,colors,'filled');
title(sprintf('Time=%.2f',t));
drawnow;
for i=1:N
w=randi([0,1],1);
if state(i)==1
dx=randi([-360,360],1);
dy=randi([-50,50],1);
x(i)=x(i)+dx;
y(i)=y(i)+dy;
quiver(x(i),y(i))
end
end
x=mod(x,L);
y=mod(y,L);
for i=1:N
w_1=randi([0,1],1);
if w_1 < P_a && state(i)==1
state(i)=2;
end
end
state=firststateupdate(N,P_ub,state); % Function Call
Displacement=sqrt((mean(x.^2))/N);
end
Also refer the modified function ‘firststateupdate.m’ that you were trying to create below:
function [IS]=firststateupdate(N,P_ub,IS)
for i=1:N
w1=randi([0,1],1);
if IS(i)==2 && w1<P_ub
IS(i)=1;
end
end
end
NOTE: When working with functions, ensure that you include all the input arguments used while defining the function before yuo call it.
For more information on creating functions in MATLAB, consider going through the following link:
I hope this helps.

Categorie

Scopri di più su Animation in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by