calculating the integral with dummy variables
2 次查看(过去 30 天)
显示 更早的评论
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/145562/image.jpeg)
Hi,
I have a problem in calculating the derivative that I uploaded. I have to use dummy variables for the integral but I can't do it.
Could you help me please?
0 个评论
回答(1 个)
Mike Hosea
2014-10-1
A "dummy" variable is a variable that is integrated out of the equation. All definite integrals use "dummy" variables in their formulation, so I don't understand the question. The only thing I can think that you might mean is that you need to express the integrals in a parameterized way, i.e. in a form where the result is not a constant, rather a function of the parameters that need to be specified. That is done using anonymous functions. For example, suppose
f = @(x,c)x.^2 + c
and I need to integrate f from 0 to 1. I can do this by creating an anonymous function that binds the value of c when it is needed.
integral(@(x)f(x,c),0,1)
Now if you try to run that as-is, without first defining c, you will get the error
Undefined function or variable 'c'.
Or if c was already defined, it will use that value of c and no other. What we need to do is make this a function, and thereby defer its evaluation until we give it the c we want it to use.
qs = @(c)integral(@(x)f(x,c),0,1)
Now we can evaluate qs with different values of c:
>> qs(1)
ans =
1.333333333333333
>> qs(2)
ans =
2.333333333333334
>> qs(3)
ans =
3.333333333333334
But we can't do this yet
>> qs(1:3)
Error using +
Matrix dimensions must agree.
Error in @(x,c)x.^2+c
To make our function of c more useful in MATLAB, we need to enable it to process arrays and operate element-wise. This is done with ARRAYFUN:
>> q = @(c)arrayfun(qs,c)
q =
@(c)arrayfun(qs,c)
>> q(1:3)
ans =
1.333333333333333 2.333333333333334 3.333333333333334
So, by using function handles and deferring evaluation until the parameter values are available, we can represent the solution to a parameterized problem.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!