function val= nonlinear_class_plot(sample,color,symbs,dim,imp_dim,coef)

% Generate Data


filename1='C:/material/work/graduate_work/vs_svr/yair/new_codes';
filename2='C:/material/work/graduate_work/vs_svr/rfe';
cd(filename1);
addpath(genpath(filename2))


warning('off','MATLAB:dispatcher:InexactCaseMatch')
%n_sim=100; sample=200; dim=10; imp_dim=2; 
x_range=2^(1/imp_dim); 
y_range=1;


seeds=zeros(length(sample),1);
p=zeros(length(sample),1);
r=zeros(length(sample),1);
hand=cell(length(sample),1);
algo=svm(kernel('rbf',1));
algo.optimizer='andre';
    

for j=1:length(sample);
    seeds(j)=1;
    corrset=1:imp_dim;  
    
    loss_name='hinge_loss';
    loss_gridsel='class_loss';
    
    example_object=toy_n; 
    example_object.seed=seeds(j);
    example_object.l=sample(j);
    example_object.n=dim;
    example_object.relevant_n=imp_dim;
    example_object.coef=[coef'; zeros(dim-imp_dim,1)];

    [d,example_object_trained]=train(example_object);
    if j==1;
        cross_validation_names={'rbf','C'};        
        cross_validation_values= {[1,2,3,4,5],[0.001,0.01,0.1,1,10,100]};
        g=gridsel(param(algo,cross_validation_names,cross_validation_values),{'score=cv','score.folds=5'});
        g.loss=loss_gridsel;
        [ret_d,ret_algo]=train(g,d);
        algo=ret_algo.best;
    end

    %SVM with risk RFE

    rfe_object=risk_rfe; 
    rfe_object.child=algo;
    rfe_object.loss=loss_name;
    rfe_object.speed=dim;
    rfe_object.output_rank=1;
    rfe_object.feat=1; 

    [d_rfe,rfe_object_trained]=train(rfe_object,d);
    val=rfe_object_trained.val; ranks=rfe_object_trained.rank;
    
    val2=val(end:-1:1);
    x=1:length(val);
    err_sos=zeros(length(val),1);
    for k=1:length(val)-1;
        x0=k;
        X_x0=[ones(length(val),1),x',(x'-x0).*(x'>x0),(x'.^2-x0^2).*(x'>x0)];
        beta_hat=(X_x0'*X_x0)\(X_x0'*val2');
        err_sos(k)=sum((val2'-X_x0*beta_hat).^2);
    end
    X_full=[ones(length(val),1),x'];
    beta_full=(X_full'*X_full)\(X_full'*val2');
    err_sos(length(val))=sum((val2'-X_full*beta_full).^2);
    [c,j_imp]=min(err_sos);
    
    x0=j_imp;
    X_x0_fin=[ones(length(val),1),x',(x'-x0).*(x'>x0),(x'.^2-x0^2).*(x'>x0)];
    beta_hat_fin=inv(X_x0_fin'*X_x0_fin)*(X_x0_fin'*val2');
    ypred=X_x0_fin*beta_hat_fin;
    
    p(j)=plot(ypred, 'Color', color{j},'Linewidth',1.5);
    hold on;
    q=plot(j_imp,ypred(j_imp),'.','MarkerEdgeColor', color{j});
    set(q, 'MarkerSize',30);
    r(j)=plot(val2,symbs{j},'Color', color{j},'Linewidth',1.5,'MarkerSize',8);
    hand{j}=['n=' num2str(sample(j))];
  
    
     
end


title('Reverse Scree Graph for SVM with Gaussian Kernel','Fontsize',15);
xlabel('Cycle number','Fontsize',15);
ylabel('Value of the Objective Function','Fontsize',15);
ll=legend(r,hand,'Location','NorthWest');
set(ll,'Fontsize',15);
box off;

hold off
