hey! i am a newbee. i need to make a 3D plot . requirments are :plot y as a function of (x and t), and also plot v as a function of (x and t) . the code is given below. thanks in advance

1 回表示 (過去 30 日間)
rana zain
rana zain 2020 年 8 月 28 日
編集済み: rana zain 2020 年 8 月 28 日
Answer is supposed to look like above. but it's coming different.
close all;
clear all;
clc;
%% m
x = (0:(1/60):1); % intial data as given by problem
m = length(x);
t = zeros(1,m);
t = (0:(1/60):0.5);
t(1, 32:61) = 0;
y = 40*sin(pi*x); % function vector
theta = [1; 0.2;0.8]; %initial theta
aplha = 0.1;
lambda = 2;
X = ones(m,3);
X(:,2) = x;
X(:,3) = t;
h_theta = X*theta;
h_theta = h_theta';
% error
J = 0;
for i = 1:m
J = J + (h_theta(i)-y(i))^2;
end
J = J/(2*m) ; % error using initial theta
Reg = lambda/(2*m) * sum(theta(2:3,1));
J = J + Reg;
v = pinv(X'*X)*X'*y'; % calculated perms
x1 = (0:0.25:1);
t1 = (0:0.1:0.4);
for i = 1:length(x1)
pol(i) = v(1) + v(2)*x1(i) + v(3)*t1(i);
end
%% 3D Plot
figure()
plot3(x1,t1,pol,'-r')
pol;
%% Mesh plot 3D surface
[X,Y] = meshgrid(x1,t1);
pol = v(1) + v(2).*X + v(3).*Y;
figure()
mesh(X,Y,pol)
figure()
surf(X,Y,pol)

回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by