MATLAB: How to plot 3d graph

3d plotsgraphplotplotting

clc
clear all
A1 = 0.015; %Excitation Amplitude

A2 = 0.035;
A3 = 0.06;
l_r = 0.617; %Wave length of the road
v = 0.04107647433; %Speed(m/s)
P = l_r/v; %Period
Om = 1/P*2*pi; %Forcing Frequency
%Om = %0.07m,2m,45m/s
%Om = 9.512/(2*pi);
k_l = 26400; %Linear stiffness
m = 483; %Mass
%d = -0.1; %Stretching condition
f_n = sqrt(k_l/m)/(2*pi); %Natural frequency
%%
fig = figure();
ax = axes();
hold(ax);
% view([-53 33]);
grid on
l_array = linspace(0.1,1,100); %Excitation Amplitude
d_array = linspace(-0.1,-1,100);
T = 150;
x0 = [0,0];
xval = zeros([],1) ;
yval = zeros([],1) ;
for i=1:numel(l_array)
for i=1:numel(d_array)
l = l_array(i);
d = d_array(i);
k_s = -(k_l*(l-d))/(4*d); %Spring stiffness
f = @(t,x) [ x(2); ...
-(2*k_s*(x(1)-(A1*sin(Om*t)+A2*sin(2*Om*t)+A3*sin(3*Om*t)))* ...
(sqrt((l-d)^2 + (x(1)-(A1*sin(Om*t)+A2*sin(2*Om*t)+A3*sin(3*Om*t)))^2) - l))/ ...
(m*(sqrt((l-d)^2 + (x(1)-(A1*sin(Om*t)+A2*sin(2*Om*t)+A3*sin(3*Om*t)))^2))) ];
[t, x] = ode45(f,[100,T],x0);
Response_amp = (max(x(:,1)) - min(x(:,1)))/2;
% xval(i) = Om/(2*pi) ;
xval(i) = l;
yval(i) = Response_amp ;
zval(i) = d;
end
% plot(Om, Response_amp, '.');
end
plot3(xval,yval,zval) ;
xlabel('length of spring (m)')
ylabel('Response Amplitude (m)')
zlabel('pretension (m)')
set(gca,'FontSize',13)
Hi, all. I wish to plot 3d grpah (length of spring vs response amplitude vs pretension). If run this code, I still get 2d graph. How am I supposed to do?
Thank you for your time.

Best Answer

It is creating 3D line, but it is not visible because you run the hold(ax) at the beginning. Add the view(3) line like this to see the 3D view
fig = figure();
ax = axes();
view(3); % <--- add this
hold(ax);
Also, note that you are using i as a variable in both loops. I think this might be a mistake. Please check according to your program logic.