How to plot multiple curves in a single figure by varying a parameter?
조회 수: 3 (최근 30일)
이전 댓글 표시
I have the following equation :-

my x ranges from (-5,10).
I want to plot multiple curves (y vs x) of the following equation by varying the parameter 'c' i.e giving some discrete values of 'c'.
How do I do that using a for loop?
댓글 수: 0
채택된 답변
Star Strider
2021년 8월 13일
편집: Star Strider
2021년 8월 13일
No loop required.
Try this —
syms c x y
Eqn = y^2/2-cos(x) == c
y = solve(Eqn,y)
yfcn = matlabFunction(y, 'Vars',{x,c})
xv = linspace(-5, 10, 25);
cv = 0:5;
[X,C] = ndgrid(xv,cv);
ymtx = yfcn(X,C);
% Q1s = size(ymtx)
figure
surf(X,C,real(ymtx((1:numel(xv)),:)))
hold on
% surf(X,C,imag(ymtx((1:numel(xv)),:)))
surf(X,C,real(ymtx((1:numel(xv))+numel(xv),:)))
% surf(X,C,imag(ymtx((1:numel(xv))+numel(xv),:)))
hold off
grid on
xlabel('x')
ylabel('c')
zlabel('y(x,c)')
figure
plot(xv, real(ymtx((1:numel(xv)),:)))
hold on
plot(xv,real(ymtx((1:numel(xv))+numel(xv),:)))
hold off
grid on
xlabel('x')
ylabel('y')
legend(compose('c = %d',cv), 'Location','bestoutside')
figure
fsurf(y, [-5 10 1 5])
xlabel('x')
ylabel('c')
zlabel('y(x,c)')
Make appropriate changes to get the result you want.
EDIT — 13 Aug 2021 at 15:42)
Corrected typographical error in the second figure plot calls (originally plotted against wrong variable).
.
댓글 수: 0
추가 답변 (1개)
Matt J
2021년 8월 13일
for c=1:5
fimplicit( @(x,y) y.^2/2-cos(x) - c)
hold on
end
hold off
xlim([-5,10])
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Calculus에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




