how to do a loop and call a function

function [g1]=func2(L,D,P,r,c,a,t)
g1=(((3*c)/(P*r))*((((D*(t-a))/L))-(L/6)));
end
function [g2]=func3(L,D,P,r,c,Y,t)
g2=1-((c/P)*exp(((-3*D)/(r*L))*(t-Y)));
end
hi guys. can someone help me? i have this to funtion and i have to set all the variable L,D,P,r,c,a and Y as a input and i have to do a for loop for t from 0 to 100 using this eqution and have to set the solution when a<t<=Y the it will use the first function and when t>Y it will use the second function and when t<a the fuction will be g=0 .but i don't know how to do it. so please help me

댓글 수: 2

Stephen23
Stephen23 2020년 2월 9일
편집: Stephen23 2020년 2월 9일
"...i have to do a for loop for t from 0 to 100 ..."
Is it really required to use a loop? If the other parameters are scalar, it would be trivial to vectorize those functions.
"...but i don't know how to do it"
Use logical indexing:
liyana nadirah
liyana nadirah 2020년 2월 9일
sorry i don't get it. can you explain

댓글을 달려면 로그인하십시오.

답변 (2개)

David Hill
David Hill 2020년 2월 9일

0 개 추천

c=;
P=;
r=;
D=;
a=;
L=;
Y=;
t1=0:a;
t2=a+1:Y;
t3=Y+1:100;
g=[zeros(1,length(t1)),(((3*c)/(P*r))*((((D*(t1-a))/L))-(L/6))),1-((c/P)*exp(((-3*D)/(r*L))*(t3-Y)))];
t=0:100;
Stephen23
Stephen23 2020년 2월 9일
편집: Stephen23 2020년 2월 9일

0 개 추천

As described in the question, you will have to do something like this:
L = ???;
D = ???;
P = ???;
r = ???;
c = ???;
a = ???;
Y = ???;
t = 0:???:100;
%
N = numel(t);
g = zeros(1,N); % this already fulfills the t<a requirement.
for k = 1:N
if t(k)>Y
g(k) = func3(??????, Y, t(k));
elseif t(k)>a
g(k) = func2(??????, a, t(k));
end
end
I left a few trivial things for you to complete (you need to replace all of the ???).
Both functions will need to be saved somewhere where they are visible to this code.

댓글 수: 6

liyana nadirah
liyana nadirah 2020년 2월 9일
did i have to this in the new script?
Stephen23
Stephen23 2020년 2월 9일
편집: Stephen23 2020년 2월 9일
"did i have to this in the new script?"
That depends entirely on where you saved those two functions, which you have not told us.
  1. If you have saved both functions together in one Mfile then you can add my code within a third function inside the same Mfile (or for MATLAB versions >=R2016b by adding a script at the start of the Mfile).
  2. If you saved each function in its own separate Mfile then you can add my code to any script or function.
liyana nadirah
liyana nadirah 2020년 2월 9일
then what if i want to plot g against time and i have to do it for various of L and have to combine all the graph in one figure
"then what if i want to plot g against time and i have to do it for various of L and have to combine all the graph in one figure"
Probably the simplest approach would be to add another loop, something like this:
L_vec = [... vector of L values...];
hold off
for kk = 1:numel(L_vec)
L = L_vec(kk);
... the rest of the code
plot(t,g)
hold on
end
liyana nadirah
liyana nadirah 2020년 2월 10일
then what if i want to plot r from 0 to 1.42 and fix the t
Stephen23
Stephen23 2020년 2월 10일
"then what if i want to plot r from 0 to 1.42 and fix the t"
If you only want to vary the r value then do the same like I showed you for L.
If you want to vary multiple different values then use ndgrid to generate all permutations.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2020년 2월 9일

댓글:

2020년 2월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by