How can I get a proper double form for a symbolic matrix?

1 visualización (últimos 30 días)
PaoloV
PaoloV el 24 de Jun. de 2016
Editada: PaoloV el 24 de Jun. de 2016
How can I get a proper double form from this symbolic Matrix in MatLab? I've tried everything but I prefer not using feval or inline function as they're not recommended
This is the code to get the matrix
function T = Romberg (a, b, m, f)
T = zeros(m, m);
T = sym(T);
syms f(x) c h;
f(x) = f;
c = (subs(f,a)+subs(f,b)) / 2;
h = b - a;
T(1,1) = h * c;
som = 0 ;
n = 2;
for i = 2 : m
h = h / 2;
for r = 1 : n/2
som = som + subs(f,(a + 2*(r-1)*h));
T(i,1) = h * (c + som);
n = 2*n;
end
end
r = 1;
for j = 2 : m
r = 4*r;
for i = j : m
T(i,j) = (r * T(i, j-1) - T(i-1,j-1)/(r-1));
end
end
end
And with an input like this
Romberg(0, 1, 4, '2*x')
I get a symbolic matrix output with all the
3 * f(3)/2 + f(1)/2 + f(5)/2
I would like to have a double output.
Can you please help me? Thank you very much in advance!

Respuesta aceptada

Steven Lord
Steven Lord el 24 de Jun. de 2016
Rather than perform the calculations symbolically, why not just write a numeric Romberg integration function? Pass your f input argument as a function handle:
f = @(x) 2*x;
Romberg(0, 1, 4, f)
You'd need to replace your symbolic subs calls with function handle evaluation like f(1) but at first glance it looks like that's the only change you would need to make.
  1 comentario
PaoloV
PaoloV el 24 de Jun. de 2016
Editada: PaoloV el 24 de Jun. de 2016
Didn't know function handle! Thank you very much!
This is the final version of the function hoping this may help someone having the same problem.
I used str2func in order to pass the function as a normal string.
function T = Romberg (a, b, m, f)
%pass the function as string
f = str2func(['@(x) ' f]);
T = zeros(m, m);
c = (f(a)+f(b)) / 2;
h = b - a;
T(1,1) = h * c;
som = 0 ;
n = 2;
for i = 2 : m
h = h / 2;
for r = 1 : n/2
som = som + subs(f,(a + 2*(r-1)*h));
T(i,1) = h * (c + som);
n = 2*n;
end
end
r = 1;
for j = 2 : m
r = 4*r;
for i = j : m
T(i,j) = (r * T(i, j-1) - T(i-1,j-1)/(r-1));
end
end
end

Iniciar sesión para comentar.

Más respuestas (2)

Kiran Prasad
Kiran Prasad el 24 de Jun. de 2016
Just from my limited experience using symbolic equations to get double answers, I would suggest maybe using the double function on each of the elements at the end if using double on the whole thing won't work. If that doesn't work I would create a small new function that just converts T to numeric. You can try using sym2cell and then cell2mat as that has worked for me before. Hopefully, this helps you-I'm by no means an expert.
  1 comentario
PaoloV
PaoloV el 24 de Jun. de 2016
Thanks for your answer,
I tried at the end of the function writing
T = cell2mat(sym2cell(T));
but MatLab prompted me
Error using cell2mat (line 52)
CELL2MAT does not support cell arrays containing cell arrays or objects.
Error in Romberg (line 47)
T = cell2mat(sym2cell(T));

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 24 de Jun. de 2016
You have an input parameter named f, and then you have
syms f(x) c h;
The first part of that,
syms f(x)
is equivalent to
f = sym('f(x)');
x = sym('x');
which clobbers the meaning of f as being from the input parameter and redefines it as being symbolic. After, places where you thought you were referring to the input parameter become references to the symbolic function.
If you are deliberately ignoring the input parameter then you should use ~ instead of giving it a name, as you are going to confuse readers. And if you are not deliberately ignoring the input parameter then you should be using a different name for f(x) compared to f.
If the idea is to indicate that the input is already a symbolic function then do not declare it with syms: MATLAB would already know it is a symbolic function because it knows the data type of the value that was passed in. But in that case you might still want to syms x

Community Treasure Hunt

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

Start Hunting!

Translated by