MATLAB Answers

1

unsure about using the hold on funciton

Nevin Pang さんによって質問されました 2019 年 3 月 19 日
最新アクティビティ Nevin Pang さんによって コメントされました 2019 年 3 月 19 日
hi, i'm still new to matlab and im unsure of how to use the hold on command.
After running the code, i'd like to produce a graph with multiple results from my code but im unsure how to use the hold on function (ie; where to put within the code)
(mulitple results as in, i'm using the limits from 0-9, 9-15 etc as listed in the above code as t and the equations x1 thru 10.
t = 0:0.1:9;
N = length(t)-1;
y = (-(1.37)*t.^2)+6000; %eqn 1
%Spline Equations
%The equations are substituted into the code.
%y1 (-(1.37)*t.^2)+6000; 0<=t<=9 0:0.1:9;
%y2 (0.943*(t-9).^2)-(24.99*t+6113.91); 9<=t<=15 9:0.1:15;
%y3 (-(0.6054)*(t-15).^2)-((13.34*t)+5975.1); 15<=t<=39 15:0.1:39;
%y4 (-(6.1557)*(t-39).^2)-((960.74*t)+7254.97); 39<=t<=49 39:0.1:49;
%y5 (5.622*(t-49).^2)-((183.8*t)+12673.95); 49<=t<=57 49:0.1:57;
%y6 (2.4008*(t-57).^2)-((93.89*t)+7903.9); 57<=t<=69 57:0.1:69;
%y7 (-(12.91)*(t-69).^2)-((36.27*t)+4273.63); 69<=t<=74 69:0.1:74;
%y8 (3.091*(t-74).^2)-((92.92*t)+8324.598); 74<=t<=81 74:0.1:81;
%y9 (0.2117*(t-81).^2)-((49.64*t)+4969.84); 81<=t<=96 81:0.1:96;
%y10 (-(5.0625)*(t-96).^2)-((43.28*t)+4407.744); 96<=t<=100 96:0.1:100
% The unknowns are 3*N with ao=0 "Linear Spline"
% Filling Matrix from point matching
V = [0;zeros(2*N,1);zeros(N-1,1)];
Z = zeros(length(V),length(V));
j=1;
f=1;
for i=2:2:2*N
Z(i,f:f+2) = [t(j)^2 t(j) 1];
V(i) = y(j);
j = j+1;
Z(i+1,f:f+2) = [t(j)^2 t(j) 1];
V(i+1) = y(j);
f = f+3;
end
% Filling Matrix from smoothing condition
j=1;
l=2;
for i=2*N+2:3*N
Z(i,j:j+1) = [2*t(l) 1];
Z(i,j+3:j+4) = [-2*t(l) -1];
j = j+3;
l = l+1;
end
% Adjusting the value of a1 to be zero "Linear Spline"
Z(1,1)=1;
% Inverting and obtaining the coeffiecients, Plotting
Coeff = Z\V;
j=1;
for i=1:N
curve=@(l) Coeff(j)*l.^2+Coeff(j+1)*l+Coeff(j+2);
ezplot(curve,[t(i),t(i+1)]);
hndl=get(gca,'Children');
set(hndl,'LineWidth',2);
j=j+3;
end
scatter(t,y,50,'r','filled')
figure (1)
title('Quadratic Spline')
xlim([min(t)-2 max(t)+2]);
ylim([min(y)-2 max(y)+2]);
xlabel('x');
ylabel('y');
grid on
grid minor
hold on
i placed a hold on at the end but i am not able to collate the results on one graph.
any help would be appreciated!
thank you!!

  1 件のコメント

Adam
2019 年 3 月 19 日
You should get into the habit of always using the full syntax for hold and plotting instructions, with an explicit axes handle, e.g.
figure; hAxes = gca;
plot( hAxes,... )
hold( hAxes, 'on' )
scatter( hAxes,...)

サインイン to comment.

タグ

1 件の回答

 採用された回答

clear
clc
close all
t = 0:0.1:9;
N = length(t)-1;
y = (-(1.37)*t.^2)+6000; %eqn 1
%Spline Equations
%The equations are substituted into the code.
%y1 (-(1.37)*t.^2)+6000; 0<=t<=9 0:0.1:9;
%y2 (0.943*(t-9).^2)-(24.99*t+6113.91); 9<=t<=15 9:0.1:15;
%y3 (-(0.6054)*(t-15).^2)-((13.34*t)+5975.1); 15<=t<=39 15:0.1:39;
%y4 (-(6.1557)*(t-39).^2)-((960.74*t)+7254.97); 39<=t<=49 39:0.1:49;
%y5 (5.622*(t-49).^2)-((183.8*t)+12673.95); 49<=t<=57 49:0.1:57;
%y6 (2.4008*(t-57).^2)-((93.89*t)+7903.9); 57<=t<=69 57:0.1:69;
%y7 (-(12.91)*(t-69).^2)-((36.27*t)+4273.63); 69<=t<=74 69:0.1:74;
%y8 (3.091*(t-74).^2)-((92.92*t)+8324.598); 74<=t<=81 74:0.1:81;
%y9 (0.2117*(t-81).^2)-((49.64*t)+4969.84); 81<=t<=96 81:0.1:96;
%y10 (-(5.0625)*(t-96).^2)-((43.28*t)+4407.744); 96<=t<=100 96:0.1:100
% The unknowns are 3*N with ao=0 "Linear Spline"
% Filling Matrix from point matching
V = [0;zeros(2*N,1);zeros(N-1,1)];
Z = zeros(length(V),length(V));
j=1;
f=1;
for i=2:2:2*N
Z(i,f:f+2) = [t(j)^2 t(j) 1];
V(i) = y(j);
j = j+1;
Z(i+1,f:f+2) = [t(j)^2 t(j) 1];
V(i+1) = y(j);
f = f+3;
end
% Filling Matrix from smoothing condition
j=1;
l=2;
for i=2*N+2:3*N
Z(i,j:j+1) = [2*t(l) 1];
Z(i,j+3:j+4) = [-2*t(l) -1];
j = j+3;
l = l+1;
end
% Adjusting the value of a1 to be zero "Linear Spline"
Z(1,1)=1;
% Inverting and obtaining the coeffiecients, Plotting
Coeff = Z\V;
j=1;
fig = figure;
scatter(t,y,50,'r','filled'),hold on
for i=1:N
curve=@(l) Coeff(j)*l.^2+Coeff(j+1)*l+Coeff(j+2);
ezplot(curve,[t(i),t(i+1)]);
hndl=get(gca,'Children');
set(hndl,'LineWidth',2);
j=j+3;
end
title('Quadratic Spline')
xlim([min(t)-2 max(t)+2]);
ylim([min(y)-2 max(y)+2]);
xlabel('x');
ylabel('y');
grid on
grid minor
Please let me know if you are looking different, if you are using latest matlab you can replaceezplot with fplot and adjust color of line to single color
eg:
replace: ezplot(curve,[t(i),t(i+1)]);
fplot(curve,[t(i),t(i+1)],'color','k');

  3 件のコメント

Nevin Pang 2019 年 3 月 19 日
i am using the r2017b version
yes i am able to replace ezplot with fplot and adjust color of line to single color
ok. you can accept answer and close this issue if you don't need any further help.
Nevin Pang 2019 年 3 月 19 日
thank you for the help!

サインイン to comment.



Translated by