简体   繁体   English

MATLAB集成的匿名函数

[英]MATLAB integration of an anonymous function

I wanted to ask how can I can write this in MATLAB. 我想问一下如何在MATLAB中编写此代码。

I want to integrate fp(z) with z(0,x) . 我想将fp(z)z(0,x)集成在一起。 I tried this : 我尝试了这个:

fpz=@(z) f1x(z) ./ quadl(f1x(z),0,1);

sol=int(fpz,0,x)  --> i also tried sol=quadl(fpz,0,x)
y=solve('y=sol',x)

xf=@ (y) y ;   -->this is the function i want

where f1x=@ (x) 1 ./(x.^2+1) and fpx = @(x) f1x(x) ./ quadl(f1x,0,1); 其中f1x=@ (x) 1 ./(x.^2+1)fpx = @(x) f1x(x) ./ quadl(f1x,0,1);

but it doesn't work. 但这不起作用。


Hello,thanks for helping. 你好,谢谢你的帮助。 The problem is that i want an analytically solution and i can't get one. 问题是我需要一种分析解决方案,而我却找不到解决方案。 I want f1x to give me " 1/x^2+1" , fpx "4/pi*(1+x^2) and fpz "4ArcTan(x)/pi", instead of giving me "f1x=@ 1./(x^2+1)".. With the code you send me ,still the same problem. I managed to come into this : 我希望f1x给我“ 1 / x ^ 2 + 1”,fpx“ 4 / pi *(1 + x ^ 2)和fpz“ 4ArcTan(x)/ pi”,而不是给我“ f1x = @ 1”。 /(x ^ 2 + 1)“。使用您发送给我的代码,仍然存在相同的问题。我设法解决了这个问题:

f1x=@ (x) 1 ./(x.^2+1) fpx = @(x) f1x(x) ./ quadl(f1x,0,1) f2z=@ (z) 1 ./(z.^2+1); f1x = @(x)1 ./(x.^2+1)fpx = @(x)f1x(x)./ quadl(f1x,0,1)f2z = @(z)1 ./(z.^ 2 + 1); fpz=@(z) fpx(z) ./ quadl(f2z,0,1) sol=int(fpz(z),z,0,x) y=solve(subs('y=sol'),x) xf=@ (y) y fpz = @(z)fpx(z)./ quadl(f2z,0,1)sol = int(fpz(z),z,0,x)y = solve(subs('y = sol'),x) xf = @(y)y

The "sol" and "y=" gives me analytically answer but it is wrong because i assume f1x and fpx,fpz doesn't return into analytically expressions. “ sol”和“ y =”给我解析上的答案,但这是错误的,因为我假设f1x和fpx,fpz不会返回解析表达式。

0 Your definition of fpz doesn't make any sense; 0您对fpz的定义没有任何意义; as Marcin already said, you're trying to integrate something that isn't a function. 正如Marcin已经说过的那样,您正在尝试集成不是功能的东西。 This shouldn't be a problem for your alternative version with f2z. 对于使用f2z的替代版本,这应该不是问题。 I think the code in the original question should have had just f1x rather than f1x(z) in the first line. 我认为原始问题中的代码在第一行中应该只有f1x而不是f1x(z)。

1 Your revised version with f2z has a different problem: now in fpz you aren't actually providing the function with an argument. 1使用f2z的修订版存在另一个问题:现在在fpz中,您实际上并未为函数提供参数。

The following code seems to be the kind of thing you had in mind, and works fine for me (in MATLAB R2008a, as it happens, but none of this should be different in other versions): 以下代码似乎是您所想到的,并且对我来说很好(在MATLAB R2008a中,它发生了,但是在其他版本中应该没有什么不同):

f1x = @(x) 1 ./ (x.^2+1);
fpx = @(x) f1x(x) ./ quadl(f1x,0,1);
fpz = @(z) fpx(z) ./ quadl(fpx,0,1);

Now evaluating fpz(3), for instance, spins for about half a second (on my old slow laptop computer) and returns 0.1273. 例如,现在评估fpz(3),旋转大约半秒(在我的旧式慢速笔记本电脑上),并返回0.1273。

So I think the problems you've been having with integration of anonymous functions are just a matter of not being quite careful enough to distinguish between the function itself and a particular value of the function. 因此,我认为您在集成匿名函数时遇到的问题只是不够谨慎地区分函数本身和函数的特定值而已。

You have some further questions about the "solve" and "int" functions in the Symbolic Math Toolbox. 您对符号数学工具箱中的“解决”和“诠释”功能还有其他疑问。 You should take the following with a pinch of salt because I don't have that toolbox and am relying on the online documentation for it. 您应该谨慎处理以下内容,因为我没有该工具箱,并且依赖于在线文档。

3 You're feeding the names of functions you've defined in MATLAB to the Symbolic Math Toolbox functions. 3您会将在MATLAB中定义的函数的名称输入Symbolic Math Toolbox函数。 I don't think that is supposed to work; 我认为那不行。 "int" and "solve" expect explicit algebraic expressions, not MATLAB functions. “ int”和“ solve”期望显式的代数表达式,而不是MATLAB函数。 (And, further, your functions all use numerical integration -- quad, quadl, etc. -- and there's no possible way that the symbolic functions can do anything useful with that.) (此外,您的函数全部使用数值积分(四,四等),并且符号函数不可能对此做任何有用的事情。)

Finally: When you're asking questions of this sort, it's helpful if rather than "it doesn't work" you say how it doesn't work. 最后:当你问这种问题,这是有益的,如果不是“它不工作”你说怎么这是行不通的。 For instance, your most recent comment is much more useful ("it gives me ..." followed by the actual output you get from MATLAB). 例如,您最近的注释要有用得多(“它给了我...”,其后是从MATLAB获得的实际输出)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM