How to display the selected function from a switch case statement
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi all,
I have a switch cases statement with selects the best in terms of RMSE fitting sinusoid function to my dataset. My question is how to display in the Command window the mathematical expression of the selected function. The code is like this.
function [RMSE,ltsc] = ltsc_scoref(A,data,period,fun)
%LTSC_SCOREF Long term seasonal component score function with square error.
% Written by Rafal Weron (2009.08.18)
% Copyright (c) 2009 by Rafal Weron
N = length(data);
x = (1:N)'/period;
switch fun
case 1 % sinusoid with linear trend
ltsc = A(1).*sin( 2.*pi.*(x+A(2)) ) + A(3) + A(4)*x;
% disp('A(1).*sin( 2.*pi.*(x+A(2)) ) + A(3) + A(4)*x')
case 2 % sinusoid with cubic trend
ltsc = A(1).*sin( 2.*pi.*(x+A(2)) ) + A(3) + A(4)*x + A(5)*x.^2;
%disp('A(1).*sin( 2.*pi.*(x+A(2)) ) + A(3) + A(4)*x + A(5)*x.^2')
case 3 % sinusoid with linear trend and linear amplitude
ltsc= (A(1) + A(6)*x).*sin( 2.*pi.*(x+A(2)) ) + A(3) + A(4)*x;
%disp('A(1) + A(6)*x).*sin( 2.*pi.*(x+A(2)) ) + A(3) + A(4)*x')
case 4 % sinusoid with cubic trend and linear amplitude
ltsc = (A(1) + A(6)*x).*sin( 2.*pi.*(x+A(2)) ) + A(3) + A(4)*x + A(5)*x.^2;
%disp('(A(1) + A(6)*x).*sin( 2.*pi.*(x+A(2)) ) + A(3) + A(4)*x + A(5)*x.^2')
case 5 % 2 sinusoids with cubic trend and linear amplitude
ltsc = (A(1) + A(6)*x).*( sin( 2.*pi.*(x+A(2)) ) + sin( pi.*(x+A(2)) ) )+ A(3) + A(4)*x + A(5)*x.^2;
%disp('(A(1) + A(6)*x).*( sin( 2.*pi.*(x+A(2)) ) + sin( pi.*(x+A(2)) ) )+ A(3) + A(4)*x + A(5)*x.^2')
end
% subtract level and compute score (sum of squared errors, SSE)
RMSE = ( mean( (abs(data - ltsc)).^2 ) )^0.5;
disp(sprintf('ltsc= %g', ltsc));
end
What I would like to see is that if for example case 3 is selected: ltsc= (A(1) + A(6)*x).*sin( 2.*pi.*(x+A(2)) ) + A(3) + A(4)*x; Thanks
0 comentarios
Respuestas (2)
John D'Errico
el 22 de Nov. de 2016
Editada: John D'Errico
el 22 de Nov. de 2016
Hint:
A= [2 3 5];
disp([num2str(A(1)),'.*X + ',num2str(A(2)),'.*Y + ',num2str(A(3))])
2.*X + 3.*Y + 5
The above is only a complete guess as to what you intend. Note that it can be pretty easily extended to a general case, where you write a small helper function that takes a list of terms and coefficients, then creates a string. With just a wee bit of effort, you can make it handle a general nonlinear case. But you need to decide how generally nonlinear a function you may have.
The point is, write a simple function that will take some functional form, with a coefficient or coefficients, returning a string. Then just loop over the terms, and build up a complete string of terms. (Not difficult, since I recall doing exactly this many years ago.)
As I said, only a guess though.
0 comentarios
Ver también
Categorías
Más información sobre Linear Programming and Mixed-Integer Linear Programming en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!