Filter löschen
Filter löschen

Can I reshape a function handle? Or change the numeric format of a function handle?

3 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Guillaume
Guillaume am 6 Feb. 2018
Bearbeitet: Guillaume am 6 Feb. 2018
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 Kommentare
Mohammad Ayoub
Mohammad Ayoub am 6 Feb. 2018
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Steven Lord
Steven Lord am 6 Feb. 2018
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 Kommentar
Mohammad Ayoub
Mohammad Ayoub am 6 Feb. 2018
Yes it was obtained using matlabFunction. I tried using digits but it did not work :( thank you for your time though I appreciate it!

Melden Sie sich an, um zu kommentieren.

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by