How to plot a multivariable function with constraints
Show older comments
Hello,
I plotted a 2-variables function and I would like to highlight (or plot/intersect) only the z points for which x (k in the function) and y satisfy the following constraints: 250020*((pi/4)*k^2*y)-325.33=0
I have been trying several approaches but I cannot make it happen. I hope you can help.
Thank you!
f=@(k,y) (8120*(((pi*((((k+(2*(5*0.0026))+(2*0.0026))+(((pi*((k+(2*(5*0.0026)))/2)*((1.49*(5*0.0026))/((5*0.0026)+0.0026)))/(8*0.97*2.350))*2)+((4.5*(((pi*(k+(2*(5*0.0026))+(2*0.0026)))/24)-((2*pi()*((k+(2*(5*0.0026)))/2)*((1.49*(5*0.0026))/((5*0.0026)+0.0026)))/(24*0.97*2.350))))*2)).^2)-((((k+(2*(5*0.0026))+(2*0.0026))+(((pi*((k+(2*(5*0.0026)))/2)*((1.49*(5*0.0026))/((5*0.0026)+0.0026)))/(8*0.97*2.350))*2)+((4.5*(((pi*(k+(2*(5*0.0026))+(2*0.0026)))/24)-((2*pi()*((k+(2*(5*0.0026)))/2)*((1.49*(5*0.0026))/((5*0.0026)+0.0026)))/(24*0.97*2.350))))*2))-(2*((pi*((k+(2*(5*0.0026)))/2)*((1.49*(5*0.0026))/((5*0.0026)+0.0026)))/(8*0.97*2.350)))).^2))*y)/4)+(((2*pi()*((k+(2*(5*0.0026)))/2)*((1.49*(5*0.0026))/((5*0.0026)+0.0026)))/(24*0.97*2.350))*(4.5*(((pi*(k+(2*(5*0.0026))+(2*0.0026)))/24)-((2*pi()*((k+(2*(5*0.0026)))/2)*((1.49*(5*0.0026))/((5*0.0026)+0.0026)))/(24*0.97*2.350))))*y.*24)))+(8120*(pi*((k.^2)-((k-(2*((pi*((k+(2*(5*0.0026)))/2)*((1.49*(5*0.0026))/((5*0.0026)+0.0026)))/(8*0.97*2.350)))).^2))*y)/4)+(7450*((pi*y.*(((k+(2*(5*0.0026))).^2)-(k.^2)))/4)+(8933*pi*(0.003/2).^2*((15*(2*4))*((2*y)+((2*(pi*((k+(2*(5*0.0026))+(2*0.0026))+(4.5*(((pi*(k+(2*(5*0.0026))+(2*0.0026)))/24)-((2*pi()*((k+(2*(5*0.0026)))/2)*((1.49*(5*0.0026))/((5*0.0026)+0.0026)))/(24*0.97*2.350)))))))/8)+(4*(((pi*((k+(2*(5*0.0026))+(2*0.0026))+(4.5*(((pi*(k+(2*(5*0.0026))+(2*0.0026)))/24)-((2*pi()*((k+(2*(5*0.0026)))/2)*((1.49*(5*0.0026))/((5*0.0026)+0.0026)))/(24*0.97*2.350))))))/24)-((2*pi()*((k+(2*(5*0.0026)))/2)*((1.49*(5*0.0026))/((5*0.0026)+0.0026)))/(24*0.97*2.350))))))*3));
fsurf(f);
xlim([0.02 0.1]);
ylim([0.04 0.4]);
zlim([0 100]);
Answers (1)
Can you solve for y, as a function of k, given the constraint? (YES.)
If so, then do you really have a TWO variable problem? (NO.)
syms k y
ysol = solve(250020*((pi/4)*k^2*y)-325.33==0,y)
z = (8120*(((pi*((((k+(2*(5*0.0026))+(2*0.0026))+(((pi*((k+(2*(5*0.0026)))/2)*((1.49*(5*0.0026))/((5*0.0026)+0.0026)))/(8*0.97*2.350))*2)+((4.5*(((pi*(k+(2*(5*0.0026))+(2*0.0026)))/24)-((2*pi()*((k+(2*(5*0.0026)))/2)*((1.49*(5*0.0026))/((5*0.0026)+0.0026)))/(24*0.97*2.350))))*2)).^2)-((((k+(2*(5*0.0026))+(2*0.0026))+(((pi*((k+(2*(5*0.0026)))/2)*((1.49*(5*0.0026))/((5*0.0026)+0.0026)))/(8*0.97*2.350))*2)+((4.5*(((pi*(k+(2*(5*0.0026))+(2*0.0026)))/24)-((2*pi()*((k+(2*(5*0.0026)))/2)*((1.49*(5*0.0026))/((5*0.0026)+0.0026)))/(24*0.97*2.350))))*2))-(2*((pi*((k+(2*(5*0.0026)))/2)*((1.49*(5*0.0026))/((5*0.0026)+0.0026)))/(8*0.97*2.350)))).^2))*y)/4)+(((2*pi()*((k+(2*(5*0.0026)))/2)*((1.49*(5*0.0026))/((5*0.0026)+0.0026)))/(24*0.97*2.350))*(4.5*(((pi*(k+(2*(5*0.0026))+(2*0.0026)))/24)-((2*pi()*((k+(2*(5*0.0026)))/2)*((1.49*(5*0.0026))/((5*0.0026)+0.0026)))/(24*0.97*2.350))))*y.*24)))+(8120*(pi*((k.^2)-((k-(2*((pi*((k+(2*(5*0.0026)))/2)*((1.49*(5*0.0026))/((5*0.0026)+0.0026)))/(8*0.97*2.350)))).^2))*y)/4)+(7450*((pi*y.*(((k+(2*(5*0.0026))).^2)-(k.^2)))/4)+(8933*pi*(0.003/2).^2*((15*(2*4))*((2*y)+((2*(pi*((k+(2*(5*0.0026))+(2*0.0026))+(4.5*(((pi*(k+(2*(5*0.0026))+(2*0.0026)))/24)-((2*pi()*((k+(2*(5*0.0026)))/2)*((1.49*(5*0.0026))/((5*0.0026)+0.0026)))/(24*0.97*2.350)))))))/8)+(4*(((pi*((k+(2*(5*0.0026))+(2*0.0026))+(4.5*(((pi*(k+(2*(5*0.0026))+(2*0.0026)))/24)-((2*pi()*((k+(2*(5*0.0026)))/2)*((1.49*(5*0.0026))/((5*0.0026)+0.0026)))/(24*0.97*2.350))))))/24)-((2*pi()*((k+(2*(5*0.0026)))/2)*((1.49*(5*0.0026))/((5*0.0026)+0.0026)))/(24*0.97*2.350))))))*3));
z_k = subs(z,y,ysol)
It may look messy, but who cares?
fplot(z_k,[0.02,0.1])
xlabel 'k'
ylabel 'z'
Again, this is not a 2 variable problem, due to the constraint.
Can you plot it in 3-dimensions anyway? Well, yes.
K = linspace(0.02,0.1,100);
yfun = matlabFunction(ysol);
Y = yfun(K);
f = @(k,y) (8120*(((pi*((((k+(2*(5*0.0026))+(2*0.0026))+(((pi*((k+(2*(5*0.0026)))/2)*((1.49*(5*0.0026))/((5*0.0026)+0.0026)))/(8*0.97*2.350))*2)+((4.5*(((pi*(k+(2*(5*0.0026))+(2*0.0026)))/24)-((2*pi()*((k+(2*(5*0.0026)))/2)*((1.49*(5*0.0026))/((5*0.0026)+0.0026)))/(24*0.97*2.350))))*2)).^2)-((((k+(2*(5*0.0026))+(2*0.0026))+(((pi*((k+(2*(5*0.0026)))/2)*((1.49*(5*0.0026))/((5*0.0026)+0.0026)))/(8*0.97*2.350))*2)+((4.5*(((pi*(k+(2*(5*0.0026))+(2*0.0026)))/24)-((2*pi()*((k+(2*(5*0.0026)))/2)*((1.49*(5*0.0026))/((5*0.0026)+0.0026)))/(24*0.97*2.350))))*2))-(2*((pi*((k+(2*(5*0.0026)))/2)*((1.49*(5*0.0026))/((5*0.0026)+0.0026)))/(8*0.97*2.350)))).^2))*y)/4)+(((2*pi()*((k+(2*(5*0.0026)))/2)*((1.49*(5*0.0026))/((5*0.0026)+0.0026)))/(24*0.97*2.350))*(4.5*(((pi*(k+(2*(5*0.0026))+(2*0.0026)))/24)-((2*pi()*((k+(2*(5*0.0026)))/2)*((1.49*(5*0.0026))/((5*0.0026)+0.0026)))/(24*0.97*2.350))))*y.*24)))+(8120*(pi*((k.^2)-((k-(2*((pi*((k+(2*(5*0.0026)))/2)*((1.49*(5*0.0026))/((5*0.0026)+0.0026)))/(8*0.97*2.350)))).^2))*y)/4)+(7450*((pi*y.*(((k+(2*(5*0.0026))).^2)-(k.^2)))/4)+(8933*pi*(0.003/2).^2*((15*(2*4))*((2*y)+((2*(pi*((k+(2*(5*0.0026))+(2*0.0026))+(4.5*(((pi*(k+(2*(5*0.0026))+(2*0.0026)))/24)-((2*pi()*((k+(2*(5*0.0026)))/2)*((1.49*(5*0.0026))/((5*0.0026)+0.0026)))/(24*0.97*2.350)))))))/8)+(4*(((pi*((k+(2*(5*0.0026))+(2*0.0026))+(4.5*(((pi*(k+(2*(5*0.0026))+(2*0.0026)))/24)-((2*pi()*((k+(2*(5*0.0026)))/2)*((1.49*(5*0.0026))/((5*0.0026)+0.0026)))/(24*0.97*2.350))))))/24)-((2*pi()*((k+(2*(5*0.0026)))/2)*((1.49*(5*0.0026))/((5*0.0026)+0.0026)))/(24*0.97*2.350))))))*3));
f = str2func(vectorize(func2str(f)));
plot3(K,Y,f(K,Y),'o-')
grid on
box on
xlabel 'k'
ylabel 'y'
zlabel 'z'
4 Comments
Lorenzo Manni
on 28 Aug 2021
I think you need to think about what are variables. Can you just put the name k into an equation without ever having defined it before? For example, I see these lines:
% Diameters as a function of k
Dmo = k+(2*lm); % [m] magnets outer diameter
Dsb = k+(2*lm)+(2*lg); % [m] stator bore diameter ESTIMATE
Was k ever defined? NO. How should matlab treat those lines? It will say this:
Unrecognized function or variable 'k'.
And how about y? What is y? Yet you use it without any predefinition...
% Air Gap Flux
...
Am = (pi*Dsb*y)/(p/pf); %CHECK L % [m^2] single pole area at stator bore
So perhaps you need to look more carefully at what I did. Did I use syms there? What does syms do? READ THE HELP! Learn MATLAB, rather than just typing your equations and hoping MATLAB will understand how to interpret what you write. You may sometimes get lucky, but not often.
What I did was to define k and y as symbolic unknowns.
help syms
doc syms
If you do that, then all of your variables now become parameterized by the unknowns k and y. So now you might try this (although I have no clue what you re really doing in this, what is your goal):
fimplicit(y == (rhoL*(((pi*((Dso^2)-((Dso-(2*lsy))^2))*y)/4)+(tw*Sd*y*Nsl)))+(rhoL*(pi*((k^2)-((k-(2*lsy))^2))*y)/4)+(rhoM*((pi*y*((Dmo^2)-(k^2)))/4)+(rhoC*pi*(Dstra/2)^2*lc*Nph)))

That is because the expression you write is an implicit function of k and y.
Lorenzo Manni
on 28 Aug 2021
Edited: Lorenzo Manni
on 28 Aug 2021
Lorenzo Manni
on 30 Aug 2021
Categories
Find more on Robotics and Mechatronics 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!

