Solving equations with different ranges

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

1 commentaire

jack carter
jack carter le 30 Août 2017
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?

Connectez-vous pour commenter.

 Réponse acceptée

Star Strider
Star Strider le 30 Août 2017
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 commentaires

Walter Roberson
Walter Roberson le 30 Août 2017
Is it certain that delta0 (S0 in the above code) is not 0? Is it certain that delta and delta0 (S and S0 in the above code) are the same sign?
jack carter
jack carter le 30 Août 2017
delta0 and delta are not the same values. Delta ranges from, say for example, 0:0.01:600; while as delta0 lies somewhere with in this range but delta0 has only single value. This single value of delta0 depends on other parameters, if i change those parameters the resultant delta0 value will be changed. But certainly will be in the range of delta.
Star Strider
Star Strider le 30 Août 2017
‘How could I use them separately later on?’
My implementation of your problem is an anonymous function. Just provide the appropriate values to it in whatever parts of your code you need its output, and it will return the necessary result. There is no need to separate out its components in various parts of your code.
Walter Roberson
Walter Roberson le 31 Août 2017
If delta can range starting from 0 to something, and delta0 is a single member of the range, then both delta and delta0 might happen to be 0, and then you would have delta/delta0 = 0/0 which will be a problem. Likewise, suppose delta is (say) 309, but delta0, being in the range 0 to 600, happens to be 10^-308, then delta/delta0 would overflow to infinity.
jack carter
jack carter le 31 Août 2017
@Star Strider I tried to run your codings but I am getting the following error. I saved the codings in checkranges.m file and the error is in line 8 which is the plot code line.
Not enough input arguments.
Error in checkranges>@(S,S0,Lf,sigma0)sigma0*((2*sqrt(S/S0)-(S/S0)).*(S<=S0)+((1-2*S/Lf).^2).*((S0<=S)&(S<=Lf/2)))
Error in checkranges (line 8) plot(S, P(S,S0))
jack carter
jack carter le 31 Août 2017
@Walter Roberson The value of delta0 will not be zero. These equations refer to the stress displacement relationship. delta is displacement and delta0 is a certain point within that displacement range.
Star Strider
Star Strider le 31 Août 2017
@jack carter — My function works if you provide all the necessary input arguments. They have to exist in your workspace before you call the function, and you have to provide the specific variables as arguments to the function.
My function cannot guess what you want it to do. You have to tell it specifically what values to use, to calculate the result it will provide.
jack carter
jack carter le 31 Août 2017
Modifié(e) : jack carter le 31 Août 2017
@Star Strider - I am not sure what I am doing wrong. I am a beginner in MATLAB. As per my understanding the variables are already existed in the workspace when I run these codes. I am able to print all of the variables with fprintf command. I have attached the coding file if you could take a look. In the command window is it like I shall type checkranges and it should work? It is working like this for printing the values of all the variables except P.
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])

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 30 Août 2017

0 votes

If you have the symbolic toolbox, you could code in terms of piecewise()

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by