Im Having issues making the IF conditions read the input placed from options.
1 view (last 30 days)
Show older comments
clear,close,clc
X=[1 5 6 7 9 11];
Y=[2 4 7 8 9 13 ];
while 1
option = input('Enter the type: (PWR/EXP/TWO/LINE) or STOP\n');
if strcmp(option,'PWR')
P_pow = polyfit(log(X),log(Y),1); % Powerfit
Y_pow = polyval(exp(P_pow), X); %Finding Y with powerfit coefficients
plot(X,Y_pow,'r*--');
fit_p= fit(X(:), Y_pow', 'poly1'); % Matlab fit function
Y_fit_p=((fit_p(1)*X+fit_p(2)));%Finding Y with fit function
elseif strcmp(option,'EXP')
Poly_exp=polyfit(exp(X), exp(Y), 2); % Exponential fit
Y_exp= polyval(Poly_exp, X);%Finding Y with exponential fit coefficients
plot(X,Y_exp,'go-');
fit_e= fit(X(:), Y_exp', 'poly1');% Matlab fit function
Y_fit_e=((fit_e(1)*X+fit_e(2)));%Finding Y with fit function
elseif strcmp(option,'TWO')
Poly_two=polyfit(X, Y, 2); % Polynomial degree 2 fit
Y_two = polyval(Poly_two, X); %Finding Y with polynomial degree 2 fit coefficients
plot(X,Y_two,'cd--');
fit_t= fit(X(:), Y_two', 'poly1');% Matlab fit function
Y_fit_t=((fit_t(1)*X+fit_t(2)));%Finding Y with fit function
elseif strcmp(option,'LINE')
Poly_Line=polyfit(X, Y, 1); % Straight line fit
Y_line = polyval(Poly_Line, X); %Finding Y with straight line fit coefficients
plot(X,Y_line,'bp-');
fit_l= fit(X(:), Y_line', 'poly1');% Matlab fit function
Y_fit_l=((fit_l(1)*X+fit_l(2)));%Finding Y with fit function
elseif strcmp(option,'STOP')
disp('Execution stopped')
break
else
disp('Try again with a valid option')
break
end
hold on
xlabel('X')
ylabel('Y')
end
disp('Row 1: MSE values from functions ')
disp('Row 2: MSE values from fit ')
[MSE]= mse(Y,Y_pow,Y_exp,Y_two,Y_line, Y_fit_p,Y_fit_e,Y_fit_t,Y_fit_l) % Function for finding MSEs
fitmin=min(MSE(1,:));
sprintf('Based on the MSE values, %d is the best fit', fitmin)
figure
Y_all=[Y_pow; Y_exp; Y_two; Y_line];
[r,c] =find(MSE==fitmin);
plot(X,Y_all(c,:))
xlabel('X')
ylabel('Y')
title('Best fit based on least MSE')
0 Comments
Answers (1)
Fangjun Jiang
on 9 Dec 2020
use option=input('prompt','s') to enter a string, not numerical data
0 Comments
See Also
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!