How to plot multiple functions on same figure using fplot?

87 views (last 30 days)
stress = [0;0.0464;0.1940;0.4962;0.5040;0.5566;0.6040;0.6260;0.6240;0.6100;0.5880;0.5720];
strain = [0;0.2220;0.3600;0.4980;0.5040;0.8820;2.6640;4.4400;5.9100;6.7380;7.1460;7.2900];
coeffL = polyfit(strain,stress,1);
mL = coeffL(1);
bL =coeffL(2);
yL = @(x) mL*x+bL;
coeffP = polyfit(log10(strain),log10(stress),1);
Warning: Polynomial is badly conditioned. Add points with distinct X values, reduce the degree of the polynomial, or try centering and scaling as described in HELP POLYFIT.
mP = coeffP(1);
bP = 10.^coeffP(2);
yP = @(x) bP.*(x.^mP);
coeffE = polyfit(strain,log(stress),1);
mE = coeffE(1);
bE = exp(coeffE(2));
yE = @(x) bE.*exp(mE.*x);
figure(2)
hold on
fplot(yL,[0,4]);
fplot(yP,[4,8]);
fplot(yE,[8,12]);
For some reason when I run the code it only graphs the linear portion

Accepted Answer

Star Strider
Star Strider on 28 Oct 2021
The problem is that the polyfit call returned NaN for both parameters. I substituted a simple nonlinear power relation and used fminsearch to solve it. The fit is not excellent, however the parameters are finite. I used those in the second plot. (The added first plot simply showed the regression result of the data and the fit.)
stress = [0;0.0464;0.1940;0.4962;0.5040;0.5566;0.6040;0.6260;0.6240;0.6100;0.5880;0.5720];
strain = [0;0.2220;0.3600;0.4980;0.5040;0.8820;2.6640;4.4400;5.9100;6.7380;7.1460;7.2900];
coeffL = polyfit(strain,stress,1);
mL = coeffL(1);
bL =coeffL(2);
yL = @(x) mL*x+bL;
objfcn = @(b,x) b(2).*x.^b(1);
B = fminsearch(@(b)norm(stress-objfcn(b,strain)), rand(2,1)*10)
B = 2×1
0.2094 0.4252
figure
plot(strain, stress, 'p')
hold on
plot(strain, objfcn(B,strain), '-r')
hold off
grid
coeffP = polyfit(log10(strain),log10(stress),1)
Warning: Polynomial is badly conditioned. Add points with distinct X values, reduce the degree of the polynomial, or try centering and scaling as described in HELP POLYFIT.
coeffP = 1×2
NaN NaN
% mP = coeffP(1);
mP = B(2);
bP = B(1);
yP = @(x) bP.*(x.^mP);
coeffE = polyfit(strain,log(stress),1);
mE = coeffE(1);
bE = exp(coeffE(2));
yE = @(x) bE.*exp(mE.*x);
figure(2)
fplot(yL,[0,4]);
hold on
fplot(yP,[4,8]);
fplot(yE,[8,12]);
Experiment to get different results.
.

More Answers (0)

Categories

Find more on Stress and Strain 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!