Can I reshape a function handle? Or change the numeric format of a function handle?
2 次查看(过去 30 天)
显示 更早的评论
Greetings.
I have created a function to solve the time-invariant state equations. The function returns three outputs which are function handles (the argument is t): x, y, and phi. When I execute the function, take only x for an example, I get n functions (n is the order of my system), so if n is 3, I get something like:
x = @(t)[exp(t.*(-6.431041321077914e-1));exp(t.*(12.4342817231314e-1));exp(t.*(-6.431041321077914e-1)).*4.35559619931756e-1-exp(t.*(-3.07978528369903e-1))]
These are three functions. The questions here are:
1. Can I change the format of the output function to be something easier to read? The numbers to the right of the decimal point are way too much and useless to me, 3 points will do. I have tried
format short
but it does not work on functions.
2. If I can't change the format, can I reshape this function to be an array of 3 functions? i.e. every function is in one row like this
x = exp(t.*(-6.431041321077914e-1))
exp(t.*(12.4342817231314e-1))
exp(t.*(-6.431041321077914e-1)).*4.35559619931756e-1-exp(t.*(-3.07978528369903e-1))
The main point here is that I am trying to plot each of these functions aside using fplot (my version is R2015b so fplot does not work with symbolic expressions), so if there is another way I am not thinking of to plot the data, it solves my problem too.
Thank you very much in advance,
M. Ayoub.
0 个评论
采纳的回答
Guillaume
2018-2-6
编辑:Guillaume
2018-2-6
There seem to be a lot of confusion in your post.
x = @(t) [something]
is a single function, regardless of the something. In your example something is a 3x1 matrix but it's irrelevant. There is only one function whose handle is stored in x.
Can I change the format of the output function
Functions do not have format. If your function uses the value -6.431041321077914e-1 with that many decimals, it's because you have written that many decimals. If you want less significant digits, write less digits. If somehow that function code was automatically generated by str2func for exmaple, then you need to change the code that put that numeric value into string, either sprintf or num2str or similar, to print the number with the required precision. format only affects the way numbers are displayed on the command line, never what is coded.
can I reshape this function to be an array of 3 functions
Again, there is only one function, which returns a 3x1 array. An array of function would be
x = {@(t) exp(t.*(-6.431041321077914e-1));
@(t) exp(t.*(12.4342817231314e-1));
@(t) exp(t.*(-6.431041321077914e-1)).*4.35559619931756e-1-exp(t.*(-3.07978528369903e-1)}
which is a completely different beast from what you have. In particular, you would not be able to just write x(somevalue) to evaluate all 3 functions at once.
The main point here is ...
I'm afraid I understand your main point or rather I don't understand what problem you are having. I suspect you need to backtrack a little and explain how you arrived at the point where you are now.
更多回答(1 个)
Steven Lord
2018-2-6
My guess is that you created your function handle using the matlabFunction function or a similar function from Symbolic Math Toolbox. Looking at the documentation it doesn't appear to have any way to control to how many decimal places the numeric values of symbolic expressions that represent numbers are written. You may want to try calling the digits function before calling matlabFunction to see if that impacts how the numbers are written in the function handle's code.
If instead you called sprintf or num2str to generate a text vector that you then converted to a function handle using str2func, change the format specifier in your sprintf or num2str calls or specify a precision in num2str.
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!