Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

Meshplot is not matching with the exact plot

1 vue (au cours des 30 derniers jours)
Asir Tushar
Asir Tushar le 2 Avr 2019
Clôturé : MATLAB Answer Bot le 20 Août 2021
I am using spectral methods on a heat equation to predict the results of a black-scholes equation but having trouble to match with the exact plot. The first plot is what is giving. but it was supposed to match with the second plot
function [D,x] = cheb(N)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
if N==0,D=0;x=1;return,end
x=cos(pi*(0:N)/N)';
c=[2;ones(N-1,1);2].*(-1).^(0:N)';
X=repmat(x,1,N+1);
dX=X-X';
D=(c*(1./c)')./(dX+(eye(N+1)));
D=D-diag(sum(D'));
end
%Differentiatio matrix and initial data
N=20; [D,x]=cheb(N);
D2=D^2;
eps=1;
dt=.01
sigma=.10
r=.12; K=100;
k=(2*r)/sigma^2;
v=max(exp(((k+1)/2)*x)-exp(((k-1)/2)*x),0)
t=0;
l=0;
%Differentition matrix and initial data;
%solve pde
tmax=1,tplot=2
nplots=round(tmax/tplot)
plotgap=round(tplot/dt),dt=tplot/plotgap
xx=0:.025:1; vv=polyval(polyfit(x,v,N),xx);
plotdata=[vv;zeros(nplots,length(xx))]; tdata=t;
for i=1:nplots
for n=1:plotgap
t=t+dt,v=v+dt*(eps*D2*v),
l=l+1
v(1)=exp(((k-1)/2+((k+1)^2)/4)*t)*(exp(1)-exp(-k*t));
v(end)=0;
end
vv=polyval(polyfit(x,v,N),xx);
plotdata(i+1,:)=vv;tdata=[tdata;t];
end
clf,subplot('position',[.1 .4 .8 .5])
mesh(xx,tdata,plotdata),grid on
axis([-1 1 0 tmax -1 2]),
view(-60,55),colormap([0 0 0]),xlabel x,ylabel t,zlabel u
spectralheat.jpg
octave-online-line-2.png

Réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by