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.

채택된 답변

Guillaume
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.
  댓글 수: 2
Mohammad Ayoub
Mohammad Ayoub 2018년 2월 6일
Thank you for your time sir, and I am sorry for the confusion I caused. I arrived at this point through complex mathematical operations like Laplace transform and and Laplace inverse transform and matrix multiplications (Matrices which contain symbolic expressions)
The data is verified from textbooks since the plot of the output function is similar to those in the books. Therefore I have no control over the decimal points because of the complexity of the operations.
As for the function array you mentioned, I just got that if I imply x(somevalue) it returns the value returned by all 3 functions. I am gonna try to plot the functions using this way, for example
L = 0:0.1:10;
Y1 = x(1,L);
Y2 = x(2,L);
Y3 = x(3,L);
plot(L,Y1,L,Y2,L,Y3)
I will try that and see if works.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
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.
  댓글 수: 1
Mohammad Ayoub
Mohammad Ayoub 2018년 2월 6일
Yes it was obtained using matlabFunction. I tried using digits but it did not work :( thank you for your time though I appreciate it!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Conversion Between Symbolic and Numeric에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by