design the function function out = mse_vs_m(x_tr, t_tr, x_te, t_te) that uses all given samples in the split data sets in matlab

main.m
function out = mse_vs_m(x_tr, t_tr, x_te, t_te)
    m_values = 1:size(x_tr,1);
    mse = [];
    for m = m_values
        w = train(x_tr, t_tr, m); % train function should be defined beforehand
        mse = [mse mean((t_te - x_te*w).^2)];
    end
    plot(m_values, mse);
    xlabel('m');
    ylabel('mse');
    title('MSE vs m');
    out = mse;
end
351 chars
14 lines

This function will take in four inputs: x_tr and t_tr represent the training input and target data, respectively, and x_te and t_te represent the testing input and target data, respectively.

The m_values variable is defined as a range of values from 1 to the number of rows (size) of the training input data x_tr, which represents the total number of basis functions that can be involved in regression.

A for loop is used to iterate over each possible value of m. At each iteration, the function trains a model using m basis functions (through the train function which is not shown in this code) on the training set, and computes the mean squared error (MSE) of the test set by taking the difference between the test set output and the actual target, squaring the differences and taking the mean. These values are stored in mse array for each m value.

Lastly, the function plots the MSE values versus the number of basis functions, and returns the resulting mse array.

gistlibby LogSnag