How to plot them together and get the equasions of each of them ?

2 views (last 30 days)
alpha=fitlm(RD,Wi);
plot(alpha);
r2=alpha.Rsquared.Adjusted;
r1=alpha.Rsquared.Ordinary;
xlabel('RD'),ylabel('ISCO');
beta=fitlm(RD,Wlr);
a=plot(beta);
xlabel('RD'),ylabel('LR');
rsqered=beta.Rsquared.Adjusted;
rsq=beta.Rsquared.Ordinary;
hold on
X=plotAdded(alpha);
hold off
% I want to know how to plot them together, with different colors, and to have a legeng for them bot.
% I also want to know the equation that describe them
% Can someone help me please??

Answers (1)

Deepak
Deepak on 15 Oct 2024
From my understanding, you have written MATLAB code to fit two linear models to two different dependent variables Wi and Wlr using the same independent variable RD. It plots each model separately and extracts R-squared values to evaluate the fit. Now, you want to combine these plots with different colors and extract their equations.
To plot them together in the same figure, we can use “plot” function in MATLAB and set the “hold on” property. We can set “Color” attribute of “plot” function to have plots of different colors. Also, we can use “legend” function to add more details to the figure.
To extract the equations from the linear models, we can use “Coefficients.Estimateproperty of the “fitlm” object that extracts the coefficients of the linear equations, which can then be formatted into strings for display.
Below is the complete MATLAB code to accomplish the task:
alpha = fitlm(RD, Wi);
beta = fitlm(RD, Wlr);
% Plot the first model
figure;
plot(alpha, 'Color', 'b');
hold on;
% Plot the second model
plot(beta, 'Color', 'r');
xlabel('RD');
ylabel('Response');
legend({'Wi Model', 'Wlr Model'}, 'Location', 'best');
title('Linear Models Comparison');
% Extract and display the equations
coeffAlpha = alpha.Coefficients.Estimate;
equationAlpha = sprintf('Wi = %.2f + %.2f*RD', coeffAlpha(1), coeffAlpha(2));
coeffBeta = beta.Coefficients.Estimate;
equationBeta = sprintf('Wlr = %.2f + %.2f*RD', coeffBeta(1), coeffBeta(2));
disp('Equations of the models:');
disp(equationAlpha);
disp(equationBeta);
hold off;
Attaching the documentation of functions used for reference:
I hope this helps to resolve the issue.

Categories

Find more on Line Plots 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!