How to get multiple function generated plots to generate from a single MATLAB script

3 visualizzazioni (ultimi 30 giorni)
I'm a student with limited MATLAB experience trying to get through a 300-level computation methods course, so bear with me. I am trying to generate multiple plots of a data set to demonstrated the curve fitting accuracy of different types of least squares regression (e.g. linear, polynomial, etc.). I have written stand alone functions for the linear regression and one for polynomical regression. In addition to calculating, I have each function generating a plot of the data with the regression curve being shown. For the assignment, we are to keep the plots separate (i.e. not all on the same plot).
My issue is that when I run my main script, I only get the plot from the last function called in the script. How can I write the main script code so that a plot for each function is generated?
Here's my linear regression function:
function [a, r2] = mylinregr_linear(x,y)
% mylinregr_linear: linear regression curve fitting
% [a, r2] = linregr(x,y): Least squares fit of straight line to data by
% solving the normal equations.
% input:
% x = independent variable
% y = dependent variable
% output:
% a = vector of slope, a(2), and intercept, a(1)
% r2 = coefficient of determination
n = length(x);
if length(y)~=n, error('x and y must be same length'); end
x = x(:); y = y(:); % convert to column vectors
sx = sum(x); sy = sum(y);
sx2 = sum(x.*x); sxy= sum(x.*y); sy2 = sum(y.*y);
a(2) = (n*sxy-sx*sy)/(n*sx2-sx^2);
a(1) = sy/n-a(2)*sx/n;
r2 = ((n*sxy-sx*sy)/sqrt(n*sx2-sx^2)/sqrt(n*sy2-sy^2))^2;
% create plot of data and best fit line
xp = linspace(min(x),max(x),2);
yp = a(2)*xp+a(1);
plot(x,y,'o',xp,yp)
grid on
Here's my polynomial/parabolic regression function:
function [a, r2] = mylinregr_parabola(x,y)
% mylinregr_parabola: polynomial regression curve fitting
% [a, r2] = linregr(x,y): Least squares fit of a parabola to data by
% solving the normal equations
% input:
% x = independent variable
% y = dependent variable
% output:
% a = coefficients of the least-squares quadratic
% r2 = coefficient of determination
n = length(x);
if length(y)~=n, error('x and y must be same length'); end
x = x(:); y = y(:); % convert to column vectors
Z = [ones(size(x)) x x.^2];
a = Z\y;
St = sum((y-mean(y)).^2);
Sr = sum((y-Z*a).^2);
r2 = 1-Sr/St;
% create plot of data and best fit line
p = polyfit(x,y,2);
xp = linspace(min(x),max(x));
yp = polyval(p,xp);
plot(x,y,'o',xp,yp)
grid on
And here's my main script:
% MAE 340 Lecture_18HW. Least-Squares Regression.
clear
clc
x = 5:5:50; % Declare x values from data set.
y = [17 24 31 33 37 37 40 40 42 41]; % Declare y values from data set.
% Run mylinregr_linear.m. Least-squares linear regression.
[a, r2] = mylinregr_linear(x,y); % <--- I get a yellow warning for this line, saying that a & r2 might not be used
% Run mylinregr_parabola.m. Least-squares linear regression.
[a, r2] = mylinregr_parabola(x,y);
Thanks in advance for taking the time to respond.

Risposta accettata

Image Analyst
Image Analyst il 5 Nov 2024
To get it to plot in a new figure, you need to call the figure() function. Otherwise it just blasts over the old/existing figure. Corrected code:
% MAE 340 Lecture_18HW. Least-Squares Regression.
clear
clc
x = 5:5:50; % Declare x values from data set.
y = [17 24 31 33 37 37 40 40 42 41]; % Declare y values from data set.
% Run mylinregr_linear.m. Least-squares linear regression.
[a, r2] = mylinregr_linear(x,y); % <--- I get a yellow warning for this line, saying that a & r2 might not be used
% Run mylinregr_parabola.m. Least-squares linear regression.
[a, r2] = mylinregr_parabola(x,y);
% Here's my linear regression function:
function [a, r2] = mylinregr_linear(x,y)
% mylinregr_linear: linear regression curve fitting
% [a, r2] = linregr(x,y): Least squares fit of straight line to data by
% solving the normal equations.
% input:
% x = independent variable
% y = dependent variable
% output:
% a = vector of slope, a(2), and intercept, a(1)
% r2 = coefficient of determination
n = length(x);
if length(y)~=n, error('x and y must be same length'); end
x = x(:); y = y(:); % convert to column vectors
sx = sum(x); sy = sum(y);
sx2 = sum(x.*x); sxy= sum(x.*y); sy2 = sum(y.*y);
a(2) = (n*sxy-sx*sy)/(n*sx2-sx^2);
a(1) = sy/n-a(2)*sx/n;
r2 = ((n*sxy-sx*sy)/sqrt(n*sx2-sx^2)/sqrt(n*sy2-sy^2))^2;
% create plot of data and best fit line
xp = linspace(min(x),max(x),2);
yp = a(2)*xp+a(1);
figure
plot(x,y,'o',xp,yp)
grid on
end
% Here's my polynomial/parabolic regression function:
function [a, r2] = mylinregr_parabola(x,y)
% mylinregr_parabola: polynomial regression curve fitting
% [a, r2] = linregr(x,y): Least squares fit of a parabola to data by
% solving the normal equations
% input:
% x = independent variable
% y = dependent variable
% output:
% a = coefficients of the least-squares quadratic
% r2 = coefficient of determination
n = length(x);
if length(y)~=n, error('x and y must be same length'); end
x = x(:); y = y(:); % convert to column vectors
Z = [ones(size(x)) x x.^2];
a = Z\y;
St = sum((y-mean(y)).^2);
Sr = sum((y-Z*a).^2);
r2 = 1-Sr/St;
% create plot of data and best fit line
p = polyfit(x,y,2);
xp = linspace(min(x),max(x));
yp = polyval(p,xp);
figure
plot(x,y,'o',xp,yp)
grid on
end
  3 Commenti
Image Analyst
Image Analyst il 7 Nov 2024
Modificato: Voss il 7 Nov 2024
@Aaron if you don't call figure, it puts stuff on the same figure, blowing away whatever was there before (unless you call "hold on" to prevent it from blowing away existing stuff). If you call figure, it creates an additional figure window where your new stuff will be plotted. And your old stuff in the other figure will still remain there. Does that make sense?

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by