Table of functions for symbolic expressions

7 visualizaciones (últimos 30 días)
Abdoulaye Thiam
Abdoulaye Thiam el 25 de Sept. de 2018
Respondida: Walter Roberson el 25 de Sept. de 2018
Hello,
I am asking how to declare a table of functions in Matlab for symbolic expressions.
The dimension of my table is n.
%Example : n=3
syms x
T(1) = x^2 ; T(2) = 2*x-1 ; T(3) = x^3 + 3 ;
Thank you

Respuesta aceptada

Dimitris Kalogiros
Dimitris Kalogiros el 25 de Sept. de 2018
It is not exactly what you asked for, but I believe that it is close enough:
close all
clearvars
syms x n
f(x,n)=piecewise(n==1, 3*x-1, n==2, x^2-1, n==3, log(x))
Look, how it works:
  1 comentario
Abdoulaye Thiam
Abdoulaye Thiam el 25 de Sept. de 2018
I am iterating a process n times, each T(i) is a symbolic expression depending on x. Then T is close to a table. Let me look at your answer.
Thanks

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 25 de Sept. de 2018
You cannot create a sym array of functions: the entries stop being functions and become just expressions.
It is possible to create an array of symfun that all have the same arguments, but it is not possible to index into it: effectively what you get is a single symfun that returns a vector or array of results. To get around this you need to use cellfun
See also
which shows additional problems if you try to create the function names dynamically.
For your purpose, I recommend creating an array of symbolic expressions (not functions!) and subs() the value of the argument into the function.
You can put symfun or sym into a table() object, with some care.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by