Where have I made mistakes in this code

%prob2.m
% u’’- 4u’ + 4u = exp(x)+ C;
% Subject to u(1) = 0; u(-1) = 0; C=(-4*exp)/(1+(exp)^2);
N = 40;
[D, x]=cheb(N); %%we call this function to get the Derivative matrix D
I = eye(N+1); % I is the identity matrix of size (N+1)
C=(-4*exp(x))/(1+(exp(x).^2)); %the value of the constant
A = D^2 - 4*D + 4*I; f = exp(x)+ C;
B = A(2:N,2:N); %to impose the boundary conditions
ff = f(2:N);
v = B\ff; %solve the BVP.
u = [0;v;0]; xx = -1:0.1:1;
u_exact = exp(xx)-(sinh(1)/sinh(2))*exp(2*xx)+ C/4;
plot(x,u); %plot the numerical solution
xlabel('x')
ylabel('u')
hold on
plot(xx,u_exact,'o'); % plot the exact solution
hold off
I have used the similar code to solve many questions, why is not working for this? Thanks. It shows this eror: Error
in prob2 (line 11)
v = B\ff; %solve the BVP.

7 件のコメント

KSSV
KSSV 2018 年 3 月 22 日
You have not mentioned what error it shows? Gives us the dimensions of B and ff along with the error.
Jos (10584)
Jos (10584) 2018 年 3 月 22 日
So, why not tell us what the actual error is! :)
Lukgaf
Lukgaf 2018 年 3 月 22 日
This is the error:
in prob2 (line 11) v = B\ff; %solve the BVP.
Lukgaf
Lukgaf 2018 年 3 月 22 日
B dimension is 39x39 whereas ff is 1x39
Stalin Samuel
Stalin Samuel 2018 年 3 月 22 日
post full error message
Lukgaf
Lukgaf 2018 年 3 月 22 日
>> prob2
Error using \
Matrix dimensions must agree.
Error in prob2 (line 11)
v = B\ff; %solve the BVP.
Jan
Jan 2018 年 3 月 22 日
編集済み: Jan 2018 年 3 月 22 日
@Lukgaf: This is the line, which causes the error. But what is the message, which explains what the problem is?

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

 採用された回答

Walter Roberson
Walter Roberson 2018 年 3 月 22 日

0 投票

v = B\ff.'; %solve the BVP.

2 件のコメント

Lukgaf
Lukgaf 2018 年 3 月 22 日
Thanks all. But why is the exact soluation has the similar problem.
Matrix dimensions must agree.
Error in prob2 (line 13)
u_exact = exp(xx)-(sinh(1)/sinh(2))* exp(2*xx)+ C/4;
Walter Roberson
Walter Roberson 2018 年 3 月 22 日
You would get that error if xx is a vector or array and C is a vector or array of different size than xx (though starting in R2016b some size combinations can be meaningfully computed as if you had used bsxfun)

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by