Multiple equations using a range of inputs for one variable then plotting a graph.

2 views (last 30 days)
Sorry about the working of the title, just a bit hard to descirbe. So basically I have a set of equations, whereby the 'main' equation relies on two other equations. I am trying to measure the value of 'y' for a range of 'r' inputs (-25 to 25). However, when doing this, the values of 'L' and 'M' are given as one single value and do not change throughout the range of 'r' inputs, even though they depend on 'r'. My current code is this:
R=25;
s=3.5;
kb=1.38e-23;
Ce=12000*kb;
r=(-25):0.00001:25;
L=((1+(12*((r.^2)/(R.^2)))+(25.2*(((r.^2)/(R.^2)).^2))+(12*(((r.^2)/(R.^2)).^3))+(((r.^2)/(R.^2)).^4))/((1-((r.^2)/(R.^2))).^10));
M=((1+((r.^2)/(R.^2)))/((1-((r.^2)/(R.^2))).^4));
u=4*Ce*((((s/R).^12)*(L)*(((r.^2)/(R.^2)).^2))-(((s/R).^6)*(M)*(((r.^2)/(R.^2)).^2)));
y=u/Ce;
plot(r,y)
When using this code, 'L' and 'M' remain as single values of '2.3131' and '1.4799', respectively. 'u', and 'y', however, change through the given range of 'r' values.
I will also attach an image of the equations so they are easier to visualise.
Thanks for any help.
Capture.PNG

Answers (1)

John D'Errico
John D'Errico on 11 Oct 2019
Edited: John D'Errico on 11 Oct 2019
Sorry, but your question makes no sense, at least not at first glance. If you want help, then clearly explain what you are doing.
In your code, it seems that you use M and L as functions now of r and R. So they are not additional equations, but merely convenient ways of writing a large equations as three shorter ones. Effectively, we just have one equation, u( r ).
We have R fixed at 25. Is R really fixed? In that case, it is only r that varies in this entire mess.
I assume that sigma in there is just s.
R=25;
s=3.5;
kb=1.38e-23;
Ce=12000*kb;
Now, write two functions for L and M. LEARN TO USE FUNCTIONS AND FUNCTION HANDLES!
Here, I will write L and M as you did, as functions of x, and then pass in r/R as x. Note they are vectorized function handles, because I carefully used the proper operators.
L = @(x) (1 + 12*x + 25.2*x.^2 + 12*x.^3 + x.^4)./(1 - x).^10;
M = @(x) (1+x)./(1-x).^4;
Next, we have u( r ). Here, r is the only variable. MATLAB uses the current values of the other parameters in your work space.
u = @(r) 4*Ce*((s/R)^12*L((r/R).^2) - (s/R)^6*M((r/R).^2));
See that still, I have never defined r to have any value. But I have defined a function u, that takes r as an argument. u is carefully vectorized too.
Finally, just use fplot.
fplot(u,[-25,25])
It should be absolutely no surprise the plot appears to be a simple pair of delta functions, singularities at r = +/- 25. However, if you reduce the range a bit, we see some non-singular behavior too.
fplot(u,[-24.5,24.5])
Again. You have just one thing to plot, but you need to learn to use functions.
Could I have plotted it using plot and a vector of values for r? Of course, although that would have take TWO lines of code at the end, instead of one line using fplot.

Categories

Find more on Line Plots in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!