Is there any method to plot two dimensional view of a function which depends on multi variables?
2 ビュー (過去 30 日間)
古いコメントを表示
suppose,
if a function is
syms x y z;
Func = x^2+y+z;
now i want to plot func with respect to x keeping y and z constant with out substituting some random constants to y and z.
1 件のコメント
Ameer Hamza
2020 年 4 月 19 日
"with out substituting some random constants to y and z". What value of y and z do you want to use? Can you show us an image of the plot similar to what you want to make?
回答 (2 件)
Thiago Henrique Gomes Lobato
2020 年 4 月 19 日
What exactly you expect to get with this? Your function dependes on three variables, so if you vary only x, the result will depend on the actual values of y and z, it doesn't make much sense to consider the value of a three dimensional function in respect to variations in x "without substituing some random constants to y and z".
In your specific case the relation of the variables is linear and the three values are completely independent from each other. In this way you could plot a function variation in respect to x. For it just select any y or z (any combination will give the same result), calculate the initial value for your x and then all the variations. Your plot will then be the 1D "x" plot minus your initial value. Because of your type of function this result will be independent of your choice of y and z.
1 件のコメント
John D'Errico
2020 年 4 月 19 日
+1. I completely, totally agree with this. Just to explain the limits of what you might do however, if the probem were just a little simpler, you could create an animated plot.
syms t x
fanimator(@fplot, x, sin((t+1)/2.*x) + t.*x/10, [0 2*pi], 'AnimationRange', [0, 10]);
playAnimation
It is animated, so I don't know if I can post the animation. I'll try the obvious though...
writeAnimation('untitled.gif')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/285751/image.gif)
Hmm. The animation did not seem to come through.
fanimator was intoduced with R2019a.
However, it is far easier in this simple case to just plot a surface, with no animation needed.
[t,x] = meshgrid(linspace(0,2*pi,100),linspace(0,10,100));
y = sin((t+1)/2.*x) + t.*x/10;
surf(x,t,y)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/285752/image.jpeg)
Or, I could just have even more easily used fsurf.
In any event, these are just ways to implicitly substitute values for the extra variables. Could I have created an animated surface? Perhaps. Odds are it would be a bit difficult to understand what you are seeing.
So you might do something, if the problem is simple enough so with essentially only a second variable. However, add too many extra variables, and the result will be an unviewable, unintelligble mess.
Ameer Hamza
2020 年 4 月 19 日
This graphic shows a demo, how the value function varies with 'x', while the value of y and z are held constant. You can use the slider to vary the values of y and z. You can adapt this code
fig = uifigure();
% create and format axes and get its handle
ax = axes(fig, 'Position', [0.1 0.25 0.8 0.7], ...
'XLim', [0 1], ...
'YLim', [0 3]);
hold(ax);
xlabel(ax,'x');
ylabel(ax,'func');
% Assign values to UserData so that it can be easily accessible inside
% the alider callback function defined later
ax.UserData.x = linspace(0,1,100);
ax.UserData.y = 0;
ax.UserData.z = 0;
ax.UserData.func = @(x,y,z) x.^2+y+z;
% draw the initial curve and get it handle. It can be later use
% to easily modify the line
f = ax.UserData.func(ax.UserData.x, ax.UserData.y, ax.UserData.z);
l = line(ax, ax.UserData.x, f, 'LineStyle', '--', 'LineWidth', 2);
% create sliders for y and z values
uis1 = uislider(fig, 'Position', [70 50 150 3], ...
'Limits', [0 1], ...
'Tag', 'y', ...
'ValueChangedFcn', {@vc_fcn, ax, l});
uis2 = uislider(fig, 'Position', [350 50 150 3], ...
'Limits', [0 1], ...
'Tag', 'z', ...
'ValueChangedFcn', {@vc_fcn, ax, l});
% create labels for y and z sliders
lbl1 = uilabel(fig, 'Position', [50 45 20 20], 'Text', 'y', ...
'FontWeight', 'bold', ...
'FontSize', 14);
lbl2 = uilabel(fig, 'Position', [330 45 20 20], 'Text', 'z', ...
'FontWeight', 'bold', ...
'FontSize', 14);
function vc_fcn(obj,~, ax, l)
if strcmp(obj.Tag, 'y')
ax.UserData.y = obj.Value;
else
ax.UserData.z = obj.Value;
end
f = ax.UserData.func(ax.UserData.x, ax.UserData.y, ax.UserData.z);
l.YData = f;
end
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Discrete Data Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!