create five random polynomials of degree n = 1, 2, 3, 4, 5 and plot them on the same figure. in matlab

main.m
%Define degree values
degrees = [1, 2, 3, 4, 5];

%Create figure
figure; hold on;

%Loop over each degree
for i = 1:length(degrees)
    %Generate random coefficients for polynomial of degree "degrees(i)"
    coeffs = rand(1, degrees(i) + 1);
    %Generate polynomial function handle
    poly = @(x) polyval(coeffs, x);
    %Plot polynomial for range [-1, 1]
    fplot(poly, [-1, 1]);
end

%Add title, xlabel, and ylabel to figure
title('Random Polynomials');
xlabel('x');
ylabel('y');
legend('Degree 1', 'Degree 2', 'Degree 3', 'Degree 4', 'Degree 5');
553 chars
22 lines

This code will create five random polynomials of degree 1, 2, 3, 4, 5 and plot them on the same figure in Matlab. The degrees variable defines the degree values for each of the polynomials. The rand function is used to generate random coefficients for each polynomial, and polyval is used to evaluate the polynomial at each x value. The fplot function is used to plot the polynomial for the range [-1, 1]. The title, xlabel, and ylabel functions are used to add labels to the figure, and the legend function is used to create a legend for each degree of polynomial. Finally, hold on is used so that all the polynomials are plotted on the same figure.

gistlibby LogSnag