Errors in passing a constant to a function in boundary value problem (bvp4c function)

1 回表示 (過去 30 日間)
Hello all, I am trying to solve a boundary value problem and I wish to pass value of variable E to odefunction() and bcfunction(), instead of declaring E separately in 2 different functions. Anyone has any suggestion? Or can't we pass constants for the function?
function [] = bvp3
% AEd2y/dx2 + 10Ax = 0; 0<x<2
%y(0)=1e-4, E*(dy/dx)=10 x=2
clc;
clear all;
close all;
%%function script
xmin=0;
xmax=2;
A=100;
%global E
E=2e5;
solinit = bvpinit(linspace(xmin,xmax,100),[-5,5]);
solution=bvp4c(@odefunction,@bcfunction,solinit);
xvar=linspace(xmin,xmax,1000);
desolution=deval(solution,xvar);
figure(1);
plot(xvar,desolution(1,:),'-k');
hold on;
plot(xvar,desolution(2,:),':k');
hold off;
title('sol');
legend('y','d^{2}y/dx^{2}');
xlabel('x');
ylabel('y');
end
%%ODE function
function odevector = odefunction(x,y,E)
%E=2e5;
odevector = [y(2),(-10/E)*x];
end
%%BC function
function bcvector = bcfunction(ya,yb,E)
%E=2e5;
bcvector = [ya(1)-1e-4,yb(2)-(10/E)];
end

採用された回答

Mischa Kim
Mischa Kim 2014 年 3 月 25 日
編集済み: Mischa Kim 2014 年 3 月 25 日
Amit, replace this one command
solution = bvp4c(@odefunction,@bcfunction,solinit);
by
options = [];
solution = bvp4c(@odefunction,@bcfunction,solinit,options,E);
Of course, you can use options to control the solver via bvpset.
  3 件のコメント
Mischa Kim
Mischa Kim 2014 年 3 月 26 日
I just checked, the function works as expected with my suggested changes with the lastest releases all the way back to R2012b, see attachment.
  • What release are you working with?
  • What're the error messages?
Amit Kumar
Amit Kumar 2014 年 3 月 26 日
Hi Mischa, Thanks for your reply. Your method did work. It was mistake from my side. Thanks a lot!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeBoundary Value Problems についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by