how to display the 3d graph for this problem?

5 views (last 30 days)
i was able to plot the graph in 2d but in 3d i am unable to plot the graph around the red line
(use this code and execute and see)
clc
clear all
syms x
f = tan(pi*x/4)
fL = [0 1]
yr = 0
iL = [0 1]
finverse(f)
Volume = pi*int((finverse(f))^2,iL(1),iL(2));
disp(['Volume is: ', num2str(double(Volume))])
fx = inline(vectorize(f));
xvals = linspace(fL(1),fL(2),201);
xvalsr = fliplr(xvals);
xivals = linspace(iL(1),iL(2),201);
xivalsr = fliplr(xivals);
xlim = [fL(1)-0.5, fL(2)+0.5];
ylim = fx(xlim);
figure(1)
subplot(2,1,1)
hold on
plot(xvals, fx(xvals),'-b','LineWidth',2)
plot([yr yr],[fL(1) fL(2)],'-r','LineWidth',2)
fill([xvals, xvalsr],[fx(xvals), ones(size(xvalsr))*yr],[0.8 0.8 0.8],'FaceAlpha',0.8)
legend('Function Plot','Axis of Rotation','Filled Region')
title('Function y=f(x) and Region')
xlabel('x-axis')
ylabel('y-axis')
subplot(2,1,2)
hold on
plot(xivals,fx(xivals),'-b','LineWidth',2)
plot(-xivals,fx(xivals),'-m','LineWidth',2)
plot([iL(1) iL(2)],[yr yr],'-r','LineWidth',2)
fill([xivals, xivalsr],[fx(xivals), ones(size(xivalsr))*yr], [0.8 0.8 0.8])
fill([-xivals, -xivalsr],[ones(size(xivals))*yr, fx(xivalsr)], [1 0.8 0.8])
title('Rotated Region in xy-Plane')
xlabel('x-axis')
ylabel('y-axis')
figure(2)
[X,Y,Z] = cylinder(xivals-yr,100);
hold on
Z = iL(1)+ Z.*(iL(2)-iL(1));
surf(Z,Y,X+yr,'EdgeColor','none','FaceColor','flat','FaceAlpha',0.6)
plot([yr yr],[iL(1) iL(2)],'-r','LineWidth',2)
title('Volume generated by revolving a curve about the y-axis')
xlabel('x-axis')
ylabel('y-axis')
zlabel('Z-axis')
view(-22,32)
the code needs correction after the figure(2) line only or maybe before too i may be wrong
can anyone help please?

Accepted Answer

Star Strider
Star Strider on 30 Nov 2021
Try it with these changes —
hs = surf(X,Y,Z,'EdgeColor','none','FaceColor','flat','FaceAlpha',0.6);
hs.ZData = hs.ZData-0.5;
hs.YData = hs.YData+0.5;
rotate(hs, [1 0 0], 90)
I already made them in the code. This just nudges the cone to where it should be, and then rotates it.
syms x
f(x) = tan(pi*x/4)
f(x) = 
fL = [0 1]
fL = 1×2
0 1
yr = 0
yr = 0
iL = [0 1]
iL = 1×2
0 1
finverse(f)
ans(x) = 
Volume = pi*int((finverse(f))^2,iL(1),iL(2));
disp(['Volume is: ', num2str(double(Volume))])
Volume is: 1.2492
fx(x) = f(x); % Inline Is Not Good Programming Practise
xvals = linspace(fL(1),fL(2),201);
xvalsr = fliplr(xvals);
xivals = linspace(iL(1),iL(2),201);
xivalsr = fliplr(xivals);
xlim = [fL(1)-0.5, fL(2)+0.5];
ylim = fx(xlim);
figure(1)
subplot(2,1,1)
hold on
plot(xvals, fx(xvals),'-b','LineWidth',2)
plot([yr yr],[fL(1) fL(2)],'-r','LineWidth',2)
fill([xvals, xvalsr],[fx(xvals), ones(size(xvalsr))*yr],[0.8 0.8 0.8],'FaceAlpha',0.8)
legend('Function Plot','Axis of Rotation','Filled Region')
title('Function y=f(x) and Region')
xlabel('x-axis')
ylabel('y-axis')
subplot(2,1,2)
hold on
plot(xivals,fx(xivals),'-b','LineWidth',2)
plot(-xivals,fx(xivals),'-m','LineWidth',2)
plot([iL(1) iL(2)],[yr yr],'-r','LineWidth',2)
fill([xivals, xivalsr],[fx(xivals), ones(size(xivalsr))*yr], [0.8 0.8 0.8])
fill([-xivals, -xivalsr],[ones(size(xivals))*yr, fx(xivalsr)], [1 0.8 0.8])
title('Rotated Region in xy-Plane')
xlabel('x-axis')
ylabel('y-axis')
figure(2)
[X,Y,Z] = cylinder(xivals-yr,100);
hold on
Z = iL(1)+ Z.*(iL(2)-iL(1));
hs = surf(X,Y,Z,'EdgeColor','none','FaceColor','flat','FaceAlpha',0.6);
hs.ZData = hs.ZData-0.5;
hs.YData = hs.YData+0.5;
rotate(hs, [1 0 0], 90)
plot([yr yr],[iL(1) iL(2)],'-r','LineWidth',2)
title('Volume generated by revolving a curve about the y-axis')
xlabel('x-axis')
ylabel('y-axis')
zlabel('Z-axis')
view(-22,32)
% view(80,30)
grid on
.
  9 Comments
Star Strider
Star Strider on 1 Dec 2021
As always, my pleasure!
No worries, and my time was definitely not wasted in responding to the posted problem. I simply had a great deal of trouble understanding what the desired result is (and I am still not certain that I have).
.

Sign in to comment.

More Answers (2)

lakshmi sampath reddy Pulagum
can you just rotate the function y=tan(pi*x/4) about y-axis or x=0 and plot in 3D.?

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by