Please add one more detail to this question that later in my function I will be needed to use values of P, from both equations, separately. Like, using the resultant values of equation one (when delta (S) is less than delta0 (S0)) and multiply or add to other parameters. And also using the resultant values of P for second equation and third equation separately with some other parameters. These values will be like my input values to solve other equations. How could I use them separately later on?
Solving equations with different ranges
4 ビュー (過去 30 日間)
古いコメントを表示
I need to get the values of P by using different equations under different values of S, as shown in the attached image. I have written codings for the equations but could someone please guide me on how can I make the equations work along with the given different range of S?
sigma0=5; Lf=12; S=0:0.01:600; S0=0.18; %arbitrary values
P=sigma0*[2*sqrt(S/S0)-(S/S0)]; %Run this eq. for S=<S0 (S is less than & equal to S0)
P=sigma0*(1-2*S/Lf)^2; %Run this eq. for S0<=S<=Lf/2
P=0; %Run this eq. for S>=Lf/2
The value of S0 will be a constant value which I will get by solving another set of equation (not included in this query). All of the values I have taken are arbitrary. Later I will be plotting the resultant values of P and for corresponding values of S.
Thank you
採用された回答
Star Strider
2017 年 8 月 30 日
Using ‘logical vectors’, ‘P’ is not defined for ‘S’ greater than or equal to ‘Lf/2’, so an explicit condition for that region to be 0 is not necessary in the expression.
This seems to me to do what you want:
sigma0=5; Lf=12; S=0:0.01:600; S0=0.18; %arbitrary values
P = @(S,S0,Lf,sigma0) sigma0*((2*sqrt(S/S0)-(S/S0)).*(S<=S0) + ((1-2*S/Lf).^2).*((S0<=S) & (S<=Lf/2)));
figure(1)
plot(S, P(S,S0))
grid
xlabel('S')
ylabel('P')
axis([0 10 ylim])
This uses an anonymous function implementation of your conditional expression.
9 件のコメント
Star Strider
2017 年 8 月 31 日
The way I wrote my function, you have to supply all the input arguments. It does not automatically take any arguments from your workspace.
This works:
sigma0=5; Lf=12; S=0:0.01:600; S0=130; %arbitrary values
P = @(S,S0,Lf,sigma0) sigma0*((2*sqrt(S/S0)-(S/S0)).*(S<=S0) + ((1-2*S/Lf).^2).*((S0<=S) & (S<=Lf/2)));
figure(1)
plot(S, P(S,S0,Lf,sigma0))
grid
xlabel('S')
ylabel('P')
axis([0 10 ylim])
その他の回答 (1 件)
Walter Roberson
2017 年 8 月 30 日
If you have the symbolic toolbox, you could code in terms of piecewise()
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Get Started with MATLAB についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!