简体   繁体   中英

MatLab: identical function gets two different results

I see the following behavior in 2013a and 2014a on Windows computers:

Someone sends me a .mat file. In this file, a function fi(th, beta) is defined. On the command line I call fi for two values (actually beta is a matrix). Then, I click "edit value" for fi and don't change anything. Then, when I call fi(th, beta) again I get a completely different value.

How can this be? How can I tell what is the "correct" value?

The reason behind your issue is that when clicking edit value on a function handle, it gets updated. In your function, D is used, which was a variable during creation of the function handle. Suppose D now has another value, fi will use the new value after pressing edit value . Let me give you an example:

>> k = 2;
>> f = @(x)k*x;
>> f(4)

ans =

     8

>> k = 4;
>> f(4)

ans =

     8

In this example, k is changed after f is defined. However, f will have its original definition. When I use edit value and don't change anything, it will nevertheless use the new value of k .

>> f % only to show the function has not changed

f = 

    @(x)k*x

>> f(4)

ans =

    16

I hope this clearifies your problem.

Your question regarding the correct value thus depends on the values and function handles used inside the function handle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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