MATLAB Answers

Hyerbolic Functions causing issues with Contour Plots.

4 views (last 30 days)
Monkeyman
Monkeyman on 21 Mar 2020
Edited: John D'Errico on 21 Mar 2020
Hi, I am currently trying to create some nodal plots, unfortunately all I seem to get are empty plots. I believe that the issue might be a consequence of the use of hyperbolic cosine and sine, but I am unsure.
Please can I have some help?
format longg
prompt = {'Enter value for k:'};
dlgtitle = 'Input';
dims = [1 100];
definput = {'5'};
k = inputdlg(prompt,dlgtitle,dims,definput);%allows user to input value for k
k = str2num(k{1});
format longg
prompt = {'Enter value for l:'};
dlgtitle = 'Input';
dims = [1 100];
definput = {'5'};
l = inputdlg(prompt,dlgtitle,dims,definput);%allows user to input value for l
l = str2num(l{1});
k1=((k+(1/2))*pi)^4
l1=((l+(1/2))*pi)^4
v1 = @(x,y) cos(k1*x)-cosh(k1*x)-((sin(k1*x)-sinh(k1*x))*((cos(k1)-cosh(k1))/(sin(k1)-sinh(k1))));
v2 = @(x,y) cos(l1*y)-cosh(l1*y)-((sin(l1*y)-sinh(l1*y))*((cos(l1)-cosh(l1))/(sin(l1)-sinh(l1))));
v3 = @(x,y) cos(l1*x)-cosh(l1*x)-((sin(l1*x)-sinh(l1*x))*((cos(l1)-cosh(l1))/(sin(l1)-sinh(l1))));
v4 = @(x,y) cos(k1*y)-cosh(k1*y)-((sin(k1*y)-sinh(k1*y))*((cos(k1)-cosh(k1))/(sin(k1)-sinh(k1))));
w = @(x,y) (v1(x,y)*v2(x,y))+(v3(x,y)*v4(x,y))
f1=figure;
fcontour(w,[0 1 0 1],'LineWidth',1.5)
c = colorbar;
c.Label.String = 'Movement in X axis';
f2=figure;
fcontour(w,[0 1 0 1],'LineWidth',1.5,'fill','on')
d = colorbar;
d.Label.String = 'Movement in X axis';

  0 Comments

Sign in to comment.

Answers (2)

the cyclist
the cyclist on 21 Mar 2020
Edited: the cyclist on 21 Mar 2020
I ran the following code
k = 0.5;
l = 0.5;
k1=((k+(1/2))*pi)^4;
l1=((l+(1/2))*pi)^4;
v1 = @(x,y) cos(k1*x)-cosh(k1*x)-((sin(k1*x)-sinh(k1*x)).*((cos(k1)-cosh(k1))./(sin(k1)-sinh(k1))));
v2 = @(x,y) cos(l1*y)-cosh(l1*y)-((sin(l1*y)-sinh(l1*y)).*((cos(l1)-cosh(l1))./(sin(l1)-sinh(l1))));
v3 = @(x,y) cos(l1*x)-cosh(l1*x)-((sin(l1*x)-sinh(l1*x)).*((cos(l1)-cosh(l1))./(sin(l1)-sinh(l1))));
v4 = @(x,y) cos(k1*y)-cosh(k1*y)-((sin(k1*y)-sinh(k1*y)).*((cos(k1)-cosh(k1))./(sin(k1)-sinh(k1))));
w = @(x,y) (v1(x,y).*v2(x,y))+(v3(x,y).*v4(x,y));
f1=figure
fcontour(w,[0 1 0 1],'LineWidth',1.5)
c = colorbar;
c.Label.String = 'Movement in X axis';
f2=figure;
fcontour(w,[0 1 0 1],'LineWidth',1.5,'fill','on')
d = colorbar;
d.Label.String = 'Movement in X axis';
and got the following figure
The changes I made to your code were
  • hard-coded the inputs k and l
  • converted the division and (some) multiplication operations inside your function into element-wise instead of matrix operations
For other values of k and l (e.g. k=5, l=5), I got an empty plot as you mention. This seems to be due to the fact that your w function evaluates to NaN everywhere. I did not trace the problem more deeply than that.

  0 Comments

Sign in to comment.


John D'Errico
John D'Errico on 21 Mar 2020
Edited: John D'Errico on 21 Mar 2020
Ugh. Learn how to use MATLAB, and scrap the crapola inpuptdlg interface.
Next, learn NOT to use variable names like a lower case L, thus l. A you can see, depending on the font, this variable will certainly be confused at some time for either the number 1, of a capital letter i, thus I. If you continue down this path, expect to find inumerable bugs in your code that need to be tracked down. Other variable names that are good ones to avoid are a lower case o. Even upper case o may be a bad idea, as 0 is far too easy to confuse with it. Or, perhaps you just like code that is impossible to read or debug. Good luck with that in the future.
Anyway, you never tell us what values for k and l we should try there, so Ill just try your code be using the defaults.
k = 5;
l = 5;
k1=((k+(1/2))*pi)^4
l1=((l+(1/2))*pi)^4
Next, learn to use semi-colons on the ends of your lines. Otherwise, you just get huge amounts of crap dumped on the screen. Here, k1 and l1 are moderately large numbers. That is significant.
k1 =
89135.4063643018
l1 =
89135.4063643018
v1 = @(x,y) cos(k1*x)-cosh(k1*x)-((sin(k1*x)-sinh(k1*x))*((cos(k1)-cosh(k1))/(sin(k1)-sinh(k1))));
Just looking at v1, I see the problem. x and y are supposed to vary on the interval [0,1].
What does cos(89135X*x) look like, when viewed on the interval [0,1] in x? TRY IT! THINK ABOUT WHAT YOU ARE DOING. Don't just create a mess of code, and then wonder why the plot at the end is meaningless.
fplot(@(x) cos(k1*x),[0,1])
So a function that oscillates periodically through
k1/(2*pi)
ans =
14186.3405273834
cycles, when x varies over that span.
That is just a tiny part of the first function you have created.
Next, we have cosh(k1*x). Again, x will vary from 0 to 1.
fplot(@(x) cosh(k1*x),[0,1])
Hmm. Why don't I see anything? Again, if x varies from 0 to 1, then 89135*x varies from 0 to 89135. And what does the cosh function look like?
fplot(@cosh)
Yes. It goes to infinity. In fact, for positive arguments, cosh(u) behaves very much like exp(u)/2 for even moderate values of u. It goes to infinity exponentially fast. So how big of a number is exp(89135)/2? BIG. So the plot of cos(k1*x) over that interval just looks like nothing, because no matter how you scale the y axis, the function is growing so rapidly, the plot is meaningless.
I can go on and on. But you should get the gist of it.
v2 = @(x,y) cos(l1*y)-cosh(l1*y)-((sin(l1*y)-sinh(l1*y))*((cos(l1)-cosh(l1))/(sin(l1)-sinh(l1))));
v3 = @(x,y) cos(l1*x)-cosh(l1*x)-((sin(l1*x)-sinh(l1*x))*((cos(l1)-cosh(l1))/(sin(l1)-sinh(l1))));
v4 = @(x,y) cos(k1*y)-cosh(k1*y)-((sin(k1*y)-sinh(k1*y))*((cos(k1)-cosh(k1))/(sin(k1)-sinh(k1))));
w = @(x,y) (v1(x,y)*v2(x,y))+(v3(x,y)*v4(x,y))
...
The above functions are combinations of things that are varying so rapidly and so hugely, that the result is complete garbage. You could not intelligently view it without a monitor the size of the universe. That x and y only vary over the interval [0,1] is not relevant. And this is not just an issue of using cosh and sinh. cos(k1*x) is just as problematic over that interval.

  0 Comments

Sign in to comment.

Sign in to answer this question.


Translated by