what is wrong in this code i dont get the correct answer
xleft=0;
xright=(9*pi)/4;
N=20;
x=linspace(xleft,xright,N);
x=x';
dx=(xright-xleft)/(N-1);
yleft=1;
yright=1;
a=1;
b=-2;
c=-2;
% M is the matrix when multiplied by y gives y'' y' and y
M2=(diag(-2*ones(N,1),0)+diag(ones(N-1,1),-1)+diag(ones(N-1,1),1))/dx^2;
M1=(diag(ones(N-1,1),1)-diag(ones(N-1,1),-1))/(2*dx);
M0=eye(N);
M=a*M2+b*M1+c*M0;
%treating boundaries separate
M(1,:)=[1,zeros(1,N-1)];
M(end,:)=[zeros(1,N-1),1];
k=(sin(x))/((cos(x))+2);
k(1) = yleft;
k(end) = yright;
y=M\k

 採用された回答

Matt J
Matt J 2019 年 4 月 3 日

1 投票

is my code correct i dont get the correct answer
Then by definition, it is not correct...

8 件のコメント

dulanga
dulanga 2019 年 4 月 4 日
what is wrong in the code any help?
Matt J
Matt J 2019 年 4 月 4 日
k=(sin(x))./((cos(x))+2);
k(1) = yleft;
k(end) = yright;
y=M\k(:)
dulanga
dulanga 2019 年 4 月 4 日
thanks a lot Matt
dulanga
dulanga 2019 年 4 月 4 日
編集済み: dulanga 2019 年 4 月 4 日
what changes would you make if say the boundary chnaged from y(0)=1 to y'(0)=1?
Matt J
Matt J 2019 年 4 月 4 日
That would be the same as saying
M1(1,:)*k(:)=1
would it not?
dulanga
dulanga 2019 年 4 月 4 日
編集済み: dulanga 2019 年 4 月 4 日
yes but what if the function is y''= smthing in this case there is no m1 for instantance this question
i think the M matrix and b has to chnage altoghter but i am not sure how ?
Matt J
Matt J 2019 年 4 月 4 日
編集済み: Matt J 2019 年 4 月 4 日
Why is there no M1? The only thing that has changed is b=c=0.
dulanga
dulanga 2019 年 4 月 4 日
yes so when it goes to M equation since b is zero M1 doesnt matter anymore ?
There is a M1 yes since b*M1 and b is zero M1 holds no impact on M then ?

サインインしてコメントする。

その他の回答 (1 件)

Muhammad Ibrahim
Muhammad Ibrahim 2019 年 4 月 4 日
編集済み: Muhammad Ibrahim 2019 年 4 月 4 日

1 投票

k=(sin(x))/((cos(x))+2);
should be
k=(sin(x))./((cos(x))+2);
That small dot after sin(x) can make a big difference.
Also. read the question carefully, you might be keying the maximum value when it maybe asking minimum value.If you still have problems just ask again in this forum maybe I can help out.

1 件のコメント

dulanga
dulanga 2019 年 4 月 4 日
How would u solve this ?

サインインしてコメントする。

カテゴリ

タグ

質問済み:

2019 年 4 月 3 日

コメント済み:

2019 年 4 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by