Plot graph with different variables in loop
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Amardeep Singh
am 15 Mai 2020
Kommentiert: Amardeep Singh
am 17 Mai 2020
Hi, I am very new to matlab, i am trying my best to run a program for differnet set of values. For example i have X and multiple Y and i am trying to run my code for diffenrent set of Y. For example, for first i need the graph with X and (Y1,Y2 and Y3) and then for the next, i need the graph for X and (Y4, Y5 AND Y6). i am trying to loop the fukction but i can not get any output, i am jsut getting erroe. Any help would be appriciated, how to run the loop. i am posting my written code for the same.
%input parameters for ICAR
%inner radius
r1=.063
%outer radius
r2=.143
%height of vane
h=.127
%shear strain
x=[.05 .125 .2 .275 .350 .425 .5];
%%Control concrete
% Mix 1-0
y1=[0.19999 0.27373 0.35502 0.46733 0.56426 0.6148 0.77323];
%Mix 1-15
y2=[0.36145 0.53114 0.61001 0.83706 0.90438 1.01193 1.29474];
%Mix 1-30
y3=[0.76393 0.76999 0.90382 1.13418 1.33516 1.46713 1.70122];
hold on
scatter (x,y1);
scatter (x,y2);
scatter (x,y3);
grid on
%xlim([0.5 4.5])
title('Curve data from ICAR rheometer')
%% Fit: 'Bingham fit'.
[xData1, yData1] = prepareCurveData( x, y1 );
[xData2, yData2] = prepareCurveData( x, y2 );
[xData3, yData3] = prepareCurveData( x, y3 );
% Set up fittype and options.
ft = fittype( 'H*x+G', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = [0.349658020198746 0.632520311675498];
% Fit model to data.
[fitresult1, gof] = fit( xData1, yData1, ft, opts );
[fitresult2, gof] = fit( xData2, yData2, ft, opts );
[fitresult3, gof] = fit( xData3, yData3, ft, opts );
hold on
figure
title('Bingham parameters G, H')
hold on
h1 = plot( fitresult1,'k', xData1, yData1,'xk' );
h2 = plot( fitresult2,'r', xData2, yData2,'*r' );
h3 = plot( fitresult3,'b', xData3, yData3,'+b' );
hold off
% Label axes
legend('0 mins','','15 mins','','30 mins','','location','nw')
xlabel ('Rotaional speed (rps)')
ylabel ('Torque (Nm)')
xlim([0 0.6])
ylim([0 1.6])
grid on
now iamgine i ahve more data of Y i want to repeat this whole thing with those daya as i dont want to copy paste and edit each and every thing.
0 Kommentare
Akzeptierte Antwort
Cris LaPierre
am 15 Mai 2020
Bearbeitet: Cris LaPierre
am 15 Mai 2020
The typical way to use the same code over and over again without rewriting it is to turn it into a function. That function can either be in a separate file, or at the bottom of your script. I might do something like this. Since I don't know how many y-values you could have, I left it variable. Right now there are 5 different markers and colors defined. If you have more y-values than that, you'd have to increase the list of options accordingly.
%input parameters for ICAR
r1=.063; %inner radius
r2=.143; %outer radius
h=.127; %height of vane
%shear strain
x=[.05 .125 .2 .275 .350 .425 .5]';
%%Control concrete
y1=[0.19999 0.27373 0.35502 0.46733 0.56426 0.6148 0.77323]'; % Mix 1-0
y2=[0.36145 0.53114 0.61001 0.83706 0.90438 1.01193 1.29474]'; % Mix 1-15
y3=[0.76393 0.76999 0.90382 1.13418 1.33516 1.46713 1.70122]'; % Mix 1-30
fitICAR(x,[y1 y2 y3])
function fitICAR(x,y)
plot(x,y,'o')
title('Curve data from ICAR rheometer')
%% Fit: 'Bingham fit'
colors='krbmc';
markers = 'x*+o.';
figure
title('Bingham parameters G, H')
hold on
% Set up fittype and options.
ft = fittype( 'H*x+G', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = [0.349658020198746 0.632520311675498];
for c = 1:size(y,2)
[xData, yData] = prepareCurveData(x, y(:,c));
% Fit model to data.
fitresult = fit(xData, yData, ft, opts);
l(c) = plot(xData,yData,'LineStyle',"none",'Marker',markers(c),"Color",colors(c));
f(c) = plot(fitresult,colors(c));
end
hold off
% Label axes
legend(l,'0 mins','15 mins','30 mins','location','nw')
xlabel ('Rotaional speed (rps)')
ylabel ('Torque (Nm)')
xlim([0 0.6])
ylim([0 1.6])
grid on
end
Now if you want to run this same plotting script for y4,y5 and y6 you just have to call the function.
fitICAR(x,[y4 y5 y6])
It is worth pointing out that each y-vector needs to be a column vector (m rows x 1 columns).
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Surface and Mesh Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!