필터 지우기
필터 지우기

Different variable in function

조회 수: 3 (최근 30일)
SHARAD KUMAR UPADHYAY
SHARAD KUMAR UPADHYAY 2019년 2월 3일
댓글: SHARAD KUMAR UPADHYAY 2019년 2월 6일
function s=traorl(fun,a,b,M)
h=(b-a)/M
s=0
for k=1:(M-1)
x=a+k*h
s=s+feval(fun,x)
end
s=h*(feval(fun,a)+feval(fun,b))/2+h*s
end
%% I wants to run function as a variable x and as well as other variables like I use function, 'fun=q*t*exp(-x)'. where i use second script file as
for x=linspace(0,10);
fun=inline('q*t*(exp(-x))');
B=traorl(fun,0,1,10)
end
But when I try to run this then i found error as
'Error using inline/feval (line 22)
Not enough inputs to inline function'
Error in traorl (line 6)
s=s+feval(fun,x)
Error in Untitled (line 7)
B=traorl(fun,0,1,10)

답변 (2개)

Walter Roberson
Walter Roberson 2019년 2월 3일
inline('q*t*(exp(-x))')
will not pay any attention to the current value of x in the workspace -- and there is no q or t in the workspace for it to ignore.
That inline() call is going to create an inline function that expects 3 inputs in the order q, t, x .
You then attempt to feval() the function of three variables, passing in only a single variable.
We can guess that what you want is
syms q t
fun = @(x) q * t * exp(-x);
B=traorl(fun,0,1,10);
which will give a result such as B = (113967594646290279*q*t)/180143985094819840 . However, this pays no attention to the linspace(0,10) you are talking about.
There seems to be some confusion as to what x is. Is it the linspace value within the loop, but inside traorl it is to take on the role of what is q outside the loop ??
  댓글 수: 4
Walter Roberson
Walter Roberson 2019년 2월 3일
fun=@(x) q*(exp(-x));
tells MATLAB that at the time of the assignment, to look in the workspace to find the current value of q (the value right then) and take a copy of that, and to construct a function of with one dummy parameter named x designating the parameter passed in at the time of execution. When fun is executed, the remembered value of q will be recalled (no matter what q might have been assigned in the mean time, the remembered q will be used), and the passed in value will be used each place that x is mentioned.
This process ignores the value of x in the workspace due to the for x statement.
You are also overwriting the value of B each iteration through the loop.
syms q t
for AVariableUsedOnlyAsAnIndex=0:10
FunctionThatDoesNotChangeWithIndexValue = @(AVariableThatHasNothingToDoWithTheIndexVariable) q*(exp(-AVariableThatHasNothingToDoWithTheIndexVariable));
B(AVariableUsedOnlyAsAnIndex+1) = traorl(FunctionThatDoesNotChangeWithIndexValue, 0, 1, 10);
end
and equivalent:
syms q
FunctionThatDoesNotChangeWithIndexValue = @(AVariableThatHasNothingToDoWithTheIndexVariable) q*(exp(-AVariableThatHasNothingToDoWithTheIndexVariable));
for AVariableUsedOnlyAsAnIndex=0:10
B(AVariableUsedOnlyAsAnIndex+1) = traorl(FunctionThatDoesNotChangeWithIndexValue, 0, 1, 10);
end
and equivalent:
syms q
FunctionThatDoesNotChangeWithIndexValue = @(AVariableThatHasNothingToDoWithTheIndexVariable) q*(exp(-AVariableThatHasNothingToDoWithTheIndexVariable));
B(1:length(0:10)) = traorl(FunctionThatDoesNotChangeWithIndexValue, 0, 1, 10);
SHARAD KUMAR UPADHYAY
SHARAD KUMAR UPADHYAY 2019년 2월 6일
Dear sir,
Answer given is very helpful for me. Another thing is, How can I do the double integration by this program? As for eg. I have function 'fun=@(x,y) q*t*sinh(y)*exp(-x). If this program is not right then please tell me the improvements in this program.
%%
function s=traorl(fun,a,b,M)
h=(b-a)/M
s=0
for k=1:(M-1)
x=a+k*h
s=s+feval(fun,x)
end
s=h*(feval(fun,a)+feval(fun,b))/2+h*s
end

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


SHARAD KUMAR UPADHYAY
SHARAD KUMAR UPADHYAY 2019년 2월 5일
Dear sir,
Answer given is very helpful for me. Another thing is, How can I do the double integration by this program? As for eg. I have function 'fun=@(x,y) q*t*sinh(y)*exp(-x). If this program is not right then please tell me the improvements in this program.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by