Different variable in function
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
SHARAD KUMAR UPADHYAY
el 3 de Feb. de 2019
Comentada: SHARAD KUMAR UPADHYAY
el 6 de Feb. de 2019
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)
0 comentarios
Respuestas (2)
Walter Roberson
el 3 de Feb. de 2019
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 comentarios
Walter Roberson
el 3 de Feb. de 2019
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);
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!