/matlabcentral/discussions/channelsChannels Discussions2024-09-10T06:04:14Ztag:www.mathworks.com,2005:Topic/8723812024-07-31T20:34:15Z2024-08-10T04:19:46ZUsing MATLAB to find a generative equation for a sequence<p>This stems purely from some play on my part. Suppose I asked you to work with the sequence formed as 2*n*F_n + 1, where F_n is the n'th Fibonacci number? Part of me would not be surprised to find there is nothing simple we could do. But, then it costs nothing to try, to see where MATLAB can take me in an explorative sense.
n = sym(0:100).';
Fn = fibonacci(n);
Sn = 2*n.*Fn + 1;
Sn(1:10) % A few elements
For kicks, I tried asking ChatGPT. Giving it nothing more than the first 20 members of thse sequence as integers, it decided this is a Perrin sequence, and gave me a recurrence relation, but one that is in fact incorrect. Good effort from the Ai, but a fail in the end.
Is there anything I can do? Try null! (Look carefully at the array generated by Toeplitz. It is at least a pretty way to generate the matrix I needed.)
X = toeplitz(Sn,[1,zeros(1,4)]);
rank(X(5:end,:))
Hmm. So there is no linear combination of those columns that yields all zeros, since the resulting matrix was full rank.
X = toeplitz(Sn,[1,zeros(1,5)]);
rank(X(6:end,:))
But if I take it one step further, we see the above matrix is now rank deficient. What does that tell me? It says there is some simple linear combination of the columns of X(6:end,:) that always yields zero. The previous test tells me there is no shorter constant coefficient recurrence releation, using fewer terms.
null(X(6:end,:))
Let me explain what those coefficients tell me. In fact, they yield a very nice recurrence relation for the sequence S_n, not unlike the original Fibonacci sequence it was based upon.
S(n+1) = 3*S(n) - S_(n-1) - 3*S(n-2) + S(n-3) + S(n-4)
where the first 5 members of that sequence are given as [1 3 5 13 25]. So a 6 term linear constant coefficient recurrence relation. If it reminds you of the generating relation for the Fibonacci sequence, that is good, because it should. (Remember I started the sequence at n==0, IF you decide to test it out.) We can test it out, like this:
SfunM = memoize(@(N) Sfun(N));
SfunM(25)
2*25*fibonacci(sym(25)) + 1
And indeed, it works as expected.
function Sn = Sfun(n)
switch n
case 0
Sn = 1;
case 1
Sn = 3;
case 2
Sn = 5;
case 3
Sn = 13;
case 4
Sn = 25;
otherwise
Sn = Sfun(n-5) + Sfun(n-4) - 3*Sfun(n-3) - Sfun(n-2) +3*Sfun(n-1);
end
end
A beauty of this, is I started from nothing but a sequence of integers, derived from an expression where I had no rational expectation of finding a formula, and out drops something pretty. I might call this explorational mathematics.
The next step of course is to go in the other direction. That is, given the derived recurrence relation, if I substitute the formula for S_n in terms of the Fibonacci numbers, can I prove it is valid in general? (Yes.) After all, without some proof, it may fail for n larger than 100. (I'm not sure how much I can cram into a single discussion, so I'll stop at this point for now. If I see interest in the ideas here, I can proceed further. For example, what was I doing with that sequence in the first place? And of course, can I prove the relation is valid? Can I do so using MATLAB?)
(I'll be honest, starting from scratch, I'm not sure it would have been obvious to find that relation, so null was hugely useful here.)</p>John D'Erricohttps://www.mathworks.com/matlabcentral/profile/authors/869215tag:www.mathworks.com,2005:Topic/8441012024-02-02T17:41:51Z2024-07-15T15:17:31ZRead this before posting<p>Hello and a warm welcome to all! We're thrilled to have you visit our community. MATLAB Central is a place for learning, sharing, and connecting with others who share your passion for MATLAB and Simulink. To ensure you have the best experience, here are some tips to get you started:
Read the Community Guidelines: Understanding our community standards is crucial. Please take a moment to familiarize yourself with them. Keep in mind that posts not adhering to these guidelines may be flagged by moderators or other community members.
Ask Technical Questions at MATLAB Answers: If you have questions related to MathWorks products, head over to MATLAB Answers (new question form - Ask the community). It's the go-to spot for technical inquiries, with responses often provided within an hour, depending on the complexity of the question and volunteer availability. To increase your chances of a speedy reply, check out our tips on how to craft a good question (link to post on asking good questions).
Choosing the Right Channel: We offer a variety of discussion channels tailored to different contexts. Select the one that best fits your post. If you're unsure, the General channel is always a safe bet. If you feel there's a need for a new channel, we encourage you to suggest it in the Ideas channel.
Reporting Issues: If you encounter posts that violate our guidelines, please use the 🚩Flag/Report feature (found in the 3-dot menu) to bring them to our attention.
Quality Control: We strive to maintain a high standard of discussion. Accounts that post spam or too much nonsense may be subject to moderation, which can include temporary suspensions or permanent bans.
Share Your Ideas: Your feedback is invaluable. If you have suggestions on how we can improve the community or MathWorks products, the Ideas channel is the perfect place to voice your thoughts.
Enjoy yourself and have fun! We're committed to fostering a supportive and educational environment. Dive into discussions, share your expertise, and grow your knowledge. We're excited to see what you'll contribute to the community!</p>Davidhttps://www.mathworks.com/matlabcentral/profile/authors/4480925tag:www.mathworks.com,2005:Topic/8648412024-06-03T21:22:40Z2024-06-03T21:22:40ZWelcome to the Cody Discussion Channel! Please Read Before Posting<p>Hello and a warm welcome to everyone! We're excited to have you in the Cody Discussion Channel. To ensure the best possible experience for everyone, it's important to understand the types of content that are most suitable for this channel.</p><p>Content that belongs in the Cody Discussion Channel:
Tips & tricks: Discuss strategies for solving Cody problems that you've found effective.
Ideas or suggestions for improvement: Have thoughts on how to make Cody better? We'd love to hear them.
Issues: Encountering difficulties or bugs with Cody? Let us know so we can address them.
Requests for guidance: Stuck on a Cody problem? Ask for advice or hints, but make sure to show your efforts in attempting to solve the problem first.
General discussions: Anything else related to Cody that doesn't fit into the above categories.</p><p>Content that does not belong in the Cody Discussion Channel:
Comments on specific Cody problems: Examples include unclear problem descriptions or incorrect testing suites.
Comments on specific Cody solutions: For example, you find a solution creative or helpful.
Please direct such comments to the Comments section on the problem or solution page itself.</p><p>We hope the Cody discussion channel becomes a vibrant space for sharing expertise, learning new skills, and connecting with others.</p>Chen Linhttps://www.mathworks.com/matlabcentral/profile/authors/6682740tag:www.mathworks.com,2005:Topic/8314622023-11-07T18:43:52Z2023-11-09T13:55:07Z🚀 The MATLAB AI Chat Playground Has Launched<p>The MATLAB AI Chat Playground is open to everyone!
Check it out here on the community: https://www.mathworks.com/matlabcentral/playground</p><p>I just published a blog post announcing the release.</p>Hans Scharlerhttps://www.mathworks.com/matlabcentral/profile/authors/5863695tag:www.mathworks.com,2005:Topic/8591662024-05-07T00:14:14Z2024-09-10T06:04:14ZHow many code files are typically in your MATLAB Projects (scripts, functions, classes, tests)?goc3https://www.mathworks.com/matlabcentral/profile/authors/5349647tag:www.mathworks.com,2005:Topic/8546812024-04-12T16:15:52Z2024-09-09T14:07:14ZPet Day with PetWorks photo contest <p>Mari is helping Dad work.</p>Vonny Groosehttps://www.mathworks.com/matlabcentral/profile/authors/13140626tag:www.mathworks.com,2005:Topic/8725812024-08-02T20:01:59Z2024-09-09T14:05:44ZWhat Olympic sports to you like to watch most?Image Analysthttps://www.mathworks.com/matlabcentral/profile/authors/1343420tag:www.mathworks.com,2005:Topic/8660312024-06-13T19:18:16Z2024-09-08T14:47:24Z+1 for Backwards Loops!<p>Base case:
Suppose you need to do a computation many times. We are going to assume that this computation cannot be vectorized. The simplest case is to use a for loop:
number_of_elements = 1e6;
test_fcn = @(x) sqrt(x) / x;
tic
for i = 1:number_of_elements
x(i) = test_fcn(i);
end
t_forward = toc;
disp(t_forward + " seconds")
Preallocation:
This can easily be sped up by preallocating the variable that houses results:
tic
x = zeros(number_of_elements, 1);
for i = 1:number_of_elements
x(i) = test_fcn(i);
end
t_forward_prealloc = toc;
disp(t_forward_prealloc + " seconds")
In this example, preallocation speeds up the loop by a factor of about three to four (running in R2024a). Comment below if you get dramatically different results.
disp(sprintf("%.1f", t_forward / t_forward_prealloc))
Run it in reverse:
Is there a way to skip the explicit preallocation and still be fast? Indeed, there is.
clear x
tic
for i = number_of_elements:-1:1
x(i) = test_fcn(i);
end
t_backward = toc;
disp(t_backward + " seconds")
By running the loop backwards, the preallocation is implicitly performed during the first iteration and the loop runs in about the same time (within statistical noise):
disp(sprintf("%.2f", t_forward_prealloc / t_backward))
Do you get similar results when running this code? Let us know your thoughts in the comments below.
Beneficial side effect:
Have you ever had to use a for loop to delete elements from a vector? If so, keeping track of index offsets can be tricky, as deleting any element shifts all those that come after. By running the for loop in reverse, you don't need to worry about index offsets while deleting elements.</p>goc3https://www.mathworks.com/matlabcentral/profile/authors/5349647tag:www.mathworks.com,2005:Topic/8752462024-09-02T01:28:52Z2024-09-07T15:02:36ZPersonal Best Downloads Level 2 Badge<p>Has this been eliminated? I've been at 31 or 32 for 30 days for awhile, but no badge. 10 badge was automatic.</p>Steve Lenkhttps://www.mathworks.com/matlabcentral/profile/authors/17562612tag:www.mathworks.com,2005:Topic/8752162024-08-30T12:41:55Z2024-09-01T02:05:02ZUsing graphs to learn about Kaprekar constant(s)<p>D.R. Kaprekar was a self taught recreational mathematician, perhaps known mostly for some numbers that bear his name.
Today, I'll focus on Kaprekar's constant (as opposed to Kaprekar numbers.)
https://en.wikipedia.org/wiki/D._R._Kaprekar
The idea is a simple one, embodied in these 5 steps.
1. Take any 4 digit integer, reduce to its decimal digits.
2. Sort the digits in decreasing order.
3. Flip the sequence of those digits, then recompose the two sets of sorted digits into 4 digit numbers. If there were any 0 digits, they will become leading zeros on the smaller number. In this case, a leading zero is acceptable to consider a number as a 4 digit integer.
4. Subtract the two numbers, smaller from the larger. The result will always have no more than 4 decimal digits. If it is less than 1000, then presume there are leading zero digits.
5. If necessary, repeat the above operation, until the result converges to a stable result, or until you see a cycle.
Since this process is deterministic, and must always result in a new 4 digit integer, it must either terminate at either an absorbing state, or in a cycle.
For example, consider the number 6174.
7641 - 1467
We get 6174 directly back. That seems rather surprising to me. But even more interesting is you will find all 4 digit numbers (excluding the pure rep-digit nmbers) will always terminate at 6174, after at most a few steps. For example, if we start with 1234
4321 - 1234
8730 - 0378
8532 - 2358
and we see that after 3 iterations of this process, we end at 6174. Similarly, if we start with 9998, it too maps to 6174 after 5 iterations.
9998 ==> 999 ==> 8991 ==> 8082 ==> 8532 ==> 6174
Why should that happen? That is, why should 6174 always drop out in the end? Clearly, since this is a deterministic proces which always produces another 4 digit integer (Assuming we treat integers with a leading zero as 4 digit integers), we must either end in some cycle, or we must end at some absorbing state. But for all (non-pure rep-digit) starting points to end at the same place, it seems just a bit surprising.
I always like to start a problem by working on a simpler problem, and see if it gives me some intuition about the process. I'll do the same thing here, but with a pair of two digit numbers. There are 100 possible two digit numbers, since we must treat all one digit numbers as having a "tens" digit of 0.
N = (0:99)';
Next, form the Kaprekar mapping for 2 digit numbers. This is easier than you may think, since we can do it in a very few lines of code on all possible inputs.
Ndig = dec2base(N,10,2) - '0';
Nmap = sort(Ndig,2,'descend')*[10;1] - sort(Ndig,2,'ascend')*[10;1];
I'll turn it into a graph, so we can visualize what happens. It also gives me an excuse to employ a very pretty set of tools in MATLAB.
G2 = graph(N+1,Nmap+1,[],cellstr(dec2base(N,10,2)));
plot(G2)
Do you see what happens? All of the rep-digit numbers, like 11, 44, 55, etc., all map directly to 0, and they stay there, since 0 also maps into 0. We can see that in the star on the lower right.
G2cycles = cyclebasis(G2)
G2cycles{1}
All other numbers eventually end up in the cycle:
G2cycles{2}
That is
81 ==> 63 ==> 27 ==> 45 ==> 09 ==> and back to 81
looping forever.
Another way of trying to visualize what happens with 2 digit numbers is to use symbolics. Thus, if we assume any 2 digit number can be written as 10*T+U, where I'll assume T>=U, since we always sort the digits first
syms T U
(10*T + U) - (10*U+T)
So after one iteration for 2 digit numbers, the result maps ALWAYS to a new 2 digit number that is divisible by 9. And there are only 10 such 2 digit numbers that are divisible by 9. So the 2-digit case must resolve itself rather quickly.
What happens when we move to 3 digit numbers? Note that for any 3 digit number abc (without loss of generality, assume a >= b >= c) it almost looks like it reduces to the 2 digit probem, aince we have abc - cba. The middle digit will always cancel itself in the subtraction operation. Does that mean we should expect a cycle at the end, as happens with 2 digit numbers? A simple modification to our previous code will tell us the answer.
N = (0:999)';
Ndig = dec2base(N,10,3) - '0';
Nmap = sort(Ndig,2,'descend')*[100;10;1] - sort(Ndig,2,'ascend')*[100;10;1];
G3 = graph(N+1,Nmap+1,[],cellstr(dec2base(N,10,2)));
plot(G3)
This one is more difficult to visualize, since there are 1000 nodes in the graph. However, we can clearly see two disjoint groups.
We can use cyclebasis to tell us the complete story again.
G3cycles = cyclebasis(G3)
G3cycles{:}
And we see that all 3 digit numbers must either terminate at 000, or 495. For example, if we start with 181, we would see:
811 - 118
963 - 369
954 - 459
It will terminate there, forever trapped at 495. And cyclebasis tells us there are no other cycles besides the boring one at 000.
What is the maximum length of any such path to get to 495?
D3 = distances(G3,496) % Remember, MATLAB uses an index origin of 1
D3(isinf(D3)) = -inf; % some nodes can never reach 495, so they have an infinite distance
plot(D3)
The maximum number of steps to get to 495 is 6 steps.
find(D3 == 6) - 1
So the 3 digit number 100 required 6 iterations to eventually reach 495.
shortestpath(G3,101,496) - 1
I think I've rather exhausted the 3 digit case. It is time now to move to the 4 digit problem, but we've already done all the hard work. The same scheme will apply to compute a graph. And the graph theory tools do all the hard work for us.
N = (0:9999)';
Ndig = dec2base(N,10,4) - '0';
Nmap = sort(Ndig,2,'descend')*[1000;100;10;1] - sort(Ndig,2,'ascend')*[1000;100;10;1];
G4 = graph(N+1,Nmap+1,[],cellstr(dec2base(N,10,2)));
plot(G4)
cyclebasis(G4)
ans{:}
And here we see the behavior, with one stable final point, 6174 as the only non-zero ending state. There are no circular cycles as we had for the 2-digit case.
How many iterations were necessary at most before termination?
D4 = distances(G4,6175);
D4(isinf(D4)) = -inf;
plot(D4)
The plot tells the story here. The maximum number of iterations before termination is 7 for the 4 digit case.
find(D4 == 7,1,'last') - 1
shortestpath(G4,9986,6175) - 1
Can you go further? Are there 5 or 6 digit Kaprekar constants? Sadly, I have read that for more than 4 digits, things break down a bit, there is no 5 digit (or higher) Kaprekar constant.
We can verify that fact, at least for 5 digit numbers.
N = (0:99999)';
Ndig = dec2base(N,10,5) - '0';
Nmap = sort(Ndig,2,'descend')*[10000;1000;100;10;1] - sort(Ndig,2,'ascend')*[10000;1000;100;10;1];
G5 = graph(N+1,Nmap+1,[],cellstr(dec2base(N,10,2)));
plot(G5)
cyclebasis(G5)
ans{:}
The result here are 4 disjoint cycles. Of course the rep-digit cycle must always be on its own, but the other three cycles are also fully disjoint, and are of respective length 2, 4, and 4.</p>John D'Erricohttps://www.mathworks.com/matlabcentral/profile/authors/869215tag:www.mathworks.com,2005:Topic/8752232024-08-31T05:55:45Z2024-08-31T05:55:45ZI'm looking for collaboration partners for solving all The Millennium Prize Conjectures, I've found potential solutions for resolving all the Conjectures.<p>Formal Proof of Smooth Solutions for Modified Navier-Stokes Equations</p><p>1. Introduction</p><p>We address the existence and smoothness of solutions to the modified Navier-Stokes equations that incorporate frequency resonances and geometric constraints. Our goal is to prove that these modifications prevent singularities, leading to smooth solutions.</p><p>2. Mathematical Formulation</p><p>2.1 Modified Navier-Stokes Equations</p><p>Consider the Navier-Stokes equations with a frequency resonance term R(u,f)\mathbf{R}(\mathbf{u}, \mathbf{f})R(u,f) and geometric constraints:</p><p>∂u∂t+(u⋅∇)u=−∇pρ+ν∇2u+R(u,f)\frac{\partial \mathbf{u}}{\partial t} + (\mathbf{u} \cdot \nabla) \mathbf{u} = -\frac{\nabla p}{\rho} + \nu \nabla^2 \mathbf{u} + \mathbf{R}(\mathbf{u}, \mathbf{f})∂t∂u+(u⋅∇)u=−ρ∇p+ν∇2u+R(u,f)</p><p>where:</p><p>• u=u(t,x)\mathbf{u} = \mathbf{u}(t, \mathbf{x})u=u(t,x) is the velocity field.</p><p>• p=p(t,x)p = p(t, \mathbf{x})p=p(t,x) is the pressure field.</p><p>• ν\nuν is the kinematic viscosity.</p><p>• R(u,f)\mathbf{R}(\mathbf{u}, \mathbf{f})R(u,f) represents the frequency resonance effects.</p><p>• f\mathbf{f}f denotes external forces.</p><p>2.2 Boundary Conditions</p><p>The boundary conditions are:</p><p>u⋅n=0 on Γ\mathbf{u} \cdot \mathbf{n} = 0 \text{ on } \Gammau⋅n=0 on Γ</p><p>where Γ\GammaΓ represents the boundary of the domain Ω\OmegaΩ, and n\mathbf{n}n is the unit normal vector on Γ\GammaΓ.</p><p>3. Existence and Smoothness of Solutions</p><p>3.1 Initial Conditions</p><p>Assume initial conditions are smooth:</p><p>u(0)∈C∞(Ω)\mathbf{u}(0) \in C^{\infty}(\Omega)u(0)∈C∞(Ω) f∈L2(Ω)\mathbf{f} \in L^2(\Omega)f∈L2(Ω)</p><p>3.2 Energy Estimates</p><p>Define the total kinetic energy:</p><p>E(t)=12∫Ω∣u(t)∣2 dΩE(t) = \frac{1}{2} \int_{\Omega} <tt>\mathbf{u}(t)</tt>^2 \, d\OmegaE(t)=21∫Ω∣u(t)∣2dΩ</p><p>Differentiate E(t)E(t)E(t) with respect to time:</p><p>dE(t)dt=∫Ωu⋅∂u∂t dΩ\frac{dE(t)}{dt} = \int_{\Omega} \mathbf{u} \cdot \frac{\partial \mathbf{u}}{\partial t} \, d\OmegadtdE(t)=∫Ωu⋅∂t∂udΩ</p><p>Substitute the modified Navier-Stokes equation:</p><p>dE(t)dt=∫Ωu⋅[−∇pρ+ν∇2u+R] dΩ\frac{dE(t)}{dt} = \int_{\Omega} \mathbf{u} \cdot \left[ -\frac{\nabla p}{\rho} + \nu \nabla^2 \mathbf{u} + \mathbf{R} \right] \, d\OmegadtdE(t)=∫Ωu⋅[−ρ∇p+ν∇2u+R]dΩ</p><p>Using the divergence-free condition (∇⋅u=0\nabla \cdot \mathbf{u} = 0∇⋅u=0):</p><p>∫Ωu⋅∇pρ dΩ=0\int_{\Omega} \mathbf{u} \cdot \frac{\nabla p}{\rho} \, d\Omega = 0∫Ωu⋅ρ∇pdΩ=0</p><p>Thus:</p><p>dE(t)dt=−ν∫Ω∣∇u∣2 dΩ+∫Ωu⋅R dΩ\frac{dE(t)}{dt} = -\nu \int_{\Omega} <tt>\nabla \mathbf{u}</tt>^2 \, d\Omega + \int_{\Omega} \mathbf{u} \cdot \mathbf{R} \, d\OmegadtdE(t)=−ν∫Ω∣∇u∣2dΩ+∫Ωu⋅RdΩ</p><p>Assuming R\mathbf{R}R is bounded by a constant CCC:</p><p>∫Ωu⋅R dΩ≤C∫Ω∣u∣ dΩ\int_{\Omega} \mathbf{u} \cdot \mathbf{R} \, d\Omega \leq C \int_{\Omega} <tt>\mathbf{u}</tt> \, d\Omega∫Ωu⋅RdΩ≤C∫Ω∣u∣dΩ</p><p>Applying the Poincaré inequality:</p><p>∫Ω∣u∣2 dΩ≤Const⋅∫Ω∣∇u∣2 dΩ\int_{\Omega} <tt>\mathbf{u}</tt>^2 \, d\Omega \leq \text{Const} \cdot \int_{\Omega} <tt>\nabla \mathbf{u}</tt>^2 \, d\Omega∫Ω∣u∣2dΩ≤Const⋅∫Ω∣∇u∣2dΩ</p><p>Therefore:</p><p>dE(t)dt≤−ν∫Ω∣∇u∣2 dΩ+C∫Ω∣u∣ dΩ\frac{dE(t)}{dt} \leq -\nu \int_{\Omega} <tt>\nabla \mathbf{u}</tt>^2 \, d\Omega + C \int_{\Omega} <tt>\mathbf{u}</tt> \, d\OmegadtdE(t)≤−ν∫Ω∣∇u∣2dΩ+C∫Ω∣u∣dΩ</p><p>Integrate this inequality:</p><p>E(t)≤E(0)−ν∫0t∫Ω∣∇u∣2 dΩ ds+CtE(t) \leq E(0) - \nu \int_{0}^{t} \int_{\Omega} <tt>\nabla \mathbf{u}</tt>^2 \, d\Omega \, ds + C tE(t)≤E(0)−ν∫0t∫Ω∣∇u∣2dΩds+Ct</p><p>Since the first term on the right-hand side is non-positive and the second term is bounded, E(t)E(t)E(t) remains bounded.</p><p>3.3 Stability Analysis</p><p>Define the Lyapunov function:</p><p>V(u)=12∫Ω∣u∣2 dΩV(\mathbf{u}) = \frac{1}{2} \int_{\Omega} <tt>\mathbf{u}</tt>^2 \, d\OmegaV(u)=21∫Ω∣u∣2dΩ</p><p>Compute its time derivative:</p><p>dVdt=∫Ωu⋅∂u∂t dΩ=−ν∫Ω∣∇u∣2 dΩ+∫Ωu⋅R dΩ\frac{dV}{dt} = \int_{\Omega} \mathbf{u} \cdot \frac{\partial \mathbf{u}}{\partial t} \, d\Omega = -\nu \int_{\Omega} <tt>\nabla \mathbf{u}</tt>^2 \, d\Omega + \int_{\Omega} \mathbf{u} \cdot \mathbf{R} \, d\OmegadtdV=∫Ωu⋅∂t∂udΩ=−ν∫Ω∣∇u∣2dΩ+∫Ωu⋅RdΩ</p><p>Since:</p><p>dVdt≤−ν∫Ω∣∇u∣2 dΩ+C\frac{dV}{dt} \leq -\nu \int_{\Omega} <tt>\nabla \mathbf{u}</tt>^2 \, d\Omega + CdtdV≤−ν∫Ω∣∇u∣2dΩ+C</p><p>and R\mathbf{R}R is bounded, u\mathbf{u}u remains bounded and smooth.</p><p>3.4 Boundary Conditions and Regularity</p><p>Verify that the boundary conditions do not induce singularities:</p><p>u⋅n=0 on Γ\mathbf{u} \cdot \mathbf{n} = 0 \text{ on } \Gammau⋅n=0 on Γ</p><p>Apply boundary value theory ensuring that the constraints preserve regularity and smoothness.</p><p>4. Extended Simulations and Experimental Validation</p><p>4.1 Simulations</p><p>• Implement numerical simulations for diverse geometrical constraints.</p><p>• Validate solutions under various frequency resonances and geometric configurations.</p><p>4.2 Experimental Validation</p><p>• Develop physical models with capillary geometries and frequency tuning.</p><p>• Test against theoretical predictions for flow characteristics and singularity avoidance.</p><p>4.3 Validation Metrics</p><p>Ensure:</p><p>• Solution smoothness and stability.</p><p>• Accurate representation of frequency and geometric effects.</p><p>• No emergence of singularities or discontinuities.</p><p>5. Conclusion</p><p>This formal proof confirms that integrating frequency resonances and geometric constraints into the Navier-Stokes equations ensures smooth solutions. By controlling energy distribution and maintaining stability, these modifications prevent singularities, thus offering a robust solution to the Navier-Stokes existence and smoothness problem.</p>Dennishttps://www.mathworks.com/matlabcentral/profile/authors/34843465tag:www.mathworks.com,2005:Topic/7880542023-05-25T21:17:55Z2024-08-30T16:02:52ZToday, while using MATLAB, I'm going to....Adam Danzhttps://www.mathworks.com/matlabcentral/profile/authors/25613423tag:www.mathworks.com,2005:Topic/8751972024-08-29T08:02:11Z2024-08-29T12:37:03Z"corr" malfunctioning in cody<p>I've been working on some matrix problems recently(Problem 55225)
and this is my code</p><p>It turns out that "Undefined function 'corr' for input arguments of type 'double'." However, should't the input argument of "corr" be column vectors with single/double values? What's even going on there?</p>J.K043006https://www.mathworks.com/matlabcentral/profile/authors/34771680tag:www.mathworks.com,2005:Topic/8746442024-08-23T17:23:53Z2024-08-29T00:20:16ZuniqueN function would be useful<p>isequaln exists to return true when NaN==NaN.
unique treats NaN==NaN as false (as it should) requiring NaN to be replaced if NaN is not considered unique in a particular application. In my application, I am checking uniqueness of table rows using [table_unique,index_unique]=unique(table,"rows","sorted") and would prefer to keep NaN as NaN or missing in table_unique without the overhead of replacing it with a dummy value then replacing it again. Dummy values also have the risk of matching existing values in the table, requiring first finding a dummy value that is not in the table.
uniquen (similar to isequaln) would be more eloquent.
Please point out if I am missing something!</p>Chris Hooperhttps://www.mathworks.com/matlabcentral/profile/authors/6463297tag:www.mathworks.com,2005:Topic/8708912024-07-22T00:44:10Z2024-08-26T10:48:52ZCan't click Roll the Dice!<p>I am trying to earn my Intro to MATLAB badge in Cody, but I cannot click the Roll the Dice! problem. It simply is not letting me click it, therefore I cannot earn my badge. Does anyone know who I should contact or what to do?</p>Marisahttps://www.mathworks.com/matlabcentral/profile/authors/30379949tag:www.mathworks.com,2005:Topic/8715312024-07-27T07:31:12Z2024-08-26T10:44:12ZWhat do you like most about MATLAB?<p>"What are your favorite features or functionalities in MATLAB, and how have they positively impacted your projects or research? Any tips or tricks to share?</p>Muhammadhttps://www.mathworks.com/matlabcentral/profile/authors/34509187tag:www.mathworks.com,2005:Topic/8742642024-08-18T14:44:21Z2024-08-19T16:02:13ZFigures vs. uiFigures<p>So generally I want to be using uifigures over figures. For example I really like the tab group component, which can really help with organizing large numbers of plots in a manageable way. I also really prefer the look of the progress dialog, uialert, confirm, etc. That said, I run into way more bugs using uifigures. I always get a “flicker” in the axes toolbar for example. I also have matlab getting “hung” a lot more often when using uifigures.</p><p>So in general, what is recommended? Are uifigures ever going to fully replace traditional figures? Are they going to become more and more robust? Do I need a better GPU to handle graphics better? Just looking for general guidance.</p>Matthew Rademacherhttps://www.mathworks.com/matlabcentral/profile/authors/14711221tag:www.mathworks.com,2005:Topic/8741792024-08-17T11:53:11Z2024-08-17T14:37:39ZDuffing equation : Transition to Chaos<p>Following on from my previous post The Non-Chaotic Duffing Equation, now we will study the chaotic behaviour of the Duffing Equation</p><p>P.s:Any comments/advice on improving the code is welcome.
The Original Duffing Equation is the following:</p><p>Let . This implies that
Then we rewrite it as a System of First-Order Equations
Using the substitution for , the second-order equation can be transformed into the following system of first-order equations:</p><pre> Exploring the Effect of .</pre><p>% Define parameters
gamma = 0.1;
alpha = -1;
beta = 1;
delta = 0.1;
omega = 1.4;</p><p>% Define the system of equations
odeSystem = @(t, y) [y(2);
-delta*y(2) - alpha*y(1) - beta*y(1)^3 + gamma*cos(omega*t)];</p><p>% Initial conditions
y0 = [0; 0]; % x(0) = 0, v(0) = 0</p><p>% Time span
tspan = [0 200];</p><p>% Solve the system
[t, y] = ode45(odeSystem, tspan, y0);</p><p>% Plot the results
figure;
plot(t, y(:, 1));
xlabel('Time');
ylabel('x(t)');
title('Solution of the nonlinear system');
grid on;</p><p>% Plot the phase portrait
figure;
plot(y(:, 1), y(:, 2));
xlabel('x(t)');
ylabel('v(t)');
title('Phase Portrait');
grid on;</p><p>% Define the tail (e.g., last 10% of the time interval)
tail_start = floor(0.9 * length(t)); % Starting index for the tail
tail_end = length(t); % Ending index for the tail</p><p>% Plot the tail of the solution
figure;
plot(y(tail_start:tail_end, 1), y(tail_start:tail_end, 2), 'r', 'LineWidth', 1.5);
xlabel('x(t)');
ylabel('v(t)');
title('Phase Portrait - Tail of the Solution');
grid on;</p><p>% Define parameters
gamma = 0.318;
alpha = -1;
beta = 1;
delta = 0.1;
omega = 1.4;</p><p>% Define the system of equations
odeSystem = @(t, y) [y(2);
-delta*y(2) - alpha*y(1) - beta*y(1)^3 + gamma*cos(omega*t)];</p><p>% Initial conditions
y0 = [0; 0]; % x(0) = 0, v(0) = 0</p><p>% Time span
tspan = [0 800];</p><p>% Solve the system
[t, y] = ode45(odeSystem, tspan, y0);</p><p>% Plot the results
figure;
plot(t, y(:, 1));
xlabel('Time');
ylabel('x(t)');
title('Solution of the nonlinear system');
grid on;</p><p>% Plot the phase portrait
figure;
plot(y(:, 1), y(:, 2));
xlabel('x(t)');
ylabel('v(t)');
title('Phase Portrait');
grid on;</p><p>% Define the tail (e.g., last 10% of the time interval)
tail_start = floor(0.9 * length(t)); % Starting index for the tail
tail_end = length(t); % Ending index for the tail</p><p>% Plot the tail of the solution
figure;
plot(y(tail_start:tail_end, 1), y(tail_start:tail_end, 2), 'r', 'LineWidth', 1.5);
xlabel('x(t)');
ylabel('v(t)');
title('Phase Portrait - Tail of the Solution');
grid on;</p><p>% Define parameters
gamma = 0.338;
alpha = -1;
beta = 1;
delta = 0.1;
omega = 1.4;</p><p>% Define the system of equations
odeSystem = @(t, y) [y(2);
-delta*y(2) - alpha*y(1) - beta*y(1)^3 + gamma*cos(omega*t)];</p><p>% Initial conditions
y0 = [0; 0]; % x(0) = 0, v(0) = 0</p><p>% Time span with more points for better resolution
tspan = linspace(0, 200,2000); % Increase the number of points</p><p>% Solve the system
[t, y] = ode45(odeSystem, tspan, y0);</p><p>% Plot the results
figure;
plot(t, y(:, 1));
xlabel('Time');
ylabel('x(t)');
title('Solution of the nonlinear system');
grid on;</p><p>% Plot the phase portrait
figure;
plot(y(:, 1), y(:, 2));
xlabel('x(t)');
ylabel('v(t)');
title('Phase Portrait');
grid on;</p><p>% Define the tail (e.g., last 10% of the time interval)
tail_start = floor(0.9 * length(t)); % Starting index for the tail
tail_end = length(t); % Ending index for the tail</p><p>% Plot the tail of the solution
figure;
plot(y(tail_start:tail_end, 1), y(tail_start:tail_end, 2), 'r', 'LineWidth', 1.5);
xlabel('x(t)');
ylabel('v(t)');
title('Phase Portrait - Tail of the Solution');
grid on;</p><p>ax = gca;
chart = ax.Children(1);
datatip(chart,0.5581,-0.1126);</p><p>% Define parameters
gamma = 0.35;
alpha = -1;
beta = 1;
delta = 0.1;
omega = 1.4;</p><p>% Define the system of equations
odeSystem = @(t, y) [y(2);
-delta*y(2) - alpha*y(1) - beta*y(1)^3 + gamma*cos(omega*t)];</p><p>% Initial conditions
y0 = [0; 0]; % x(0) = 0, v(0) = 0</p><p>% Time span with more points for better resolution
tspan = linspace(0, 400,3000); % Increase the number of points</p><p>% Solve the system
[t, y] = ode45(odeSystem, tspan, y0);</p><p>% Plot the results
figure;
plot(t, y(:, 1));
xlabel('Time');
ylabel('x(t)');
title('Solution of the nonlinear system');
grid on;</p><p>% Plot the phase portrait
figure;
plot(y(:, 1), y(:, 2));
xlabel('x(t)');
ylabel('v(t)');
title('Phase Portrait');
grid on;</p><p>% Define the tail (e.g., last 10% of the time interval)
tail_start = floor(0.9 * length(t)); % Starting index for the tail
tail_end = length(t); % Ending index for the tail</p><p>% Plot the tail of the solution
figure;
plot(y(tail_start:tail_end, 1), y(tail_start:tail_end, 2), 'r', 'LineWidth', 1.5);
xlabel('x(t)');
ylabel('v(t)');
title('Phase Portrait - Tail of the Solution');
grid on;</p>Athanasios Paraskevopouloshttps://www.mathworks.com/matlabcentral/profile/authors/30623616tag:www.mathworks.com,2005:Topic/8740492024-08-16T11:25:15Z2024-08-16T11:25:15ZDrone for Matlab<p>Hi everyone, I am from India ..Suggest some drone for deploying code from Matlab.</p>Salam Surjithttps://www.mathworks.com/matlabcentral/profile/authors/11868646tag:www.mathworks.com,2005:Topic/8739642024-08-15T21:31:38Z2024-08-15T21:31:38ZThe Non-Chaotic Duffing Equation<p>Studying the attached document Duffing Equation from the University of Colorado, I noticed that there is an analysis of The Non-Chaotic Duffing Equation and all the graphs were created with Matlab. And since the code is not given I took the initiative to try to create the same graphs with the following code.</p><p>Plotting the Potential Energy and Identifying Extrema
% Define the range of x values
x = linspace(-2, 2, 1000);</p><p>% Define the potential function V(x)
V = -x.^2 / 2 + x.^4 / 4;</p><p>% Plot the potential function
figure;
plot(x, V, 'LineWidth', 2);
hold on;</p><p>% Mark the minima at x = ±1
plot([-1, 1], [-1/4, -1/4], 'ro', 'MarkerSize', 5, 'MarkerFaceColor', 'g');</p><p>% Add LaTeX title and labels
title('Duffing Potential Energy: $$V(x) = -\frac{x^2}{2} + \frac{x^4}{4}$$', 'Interpreter', 'latex');
xlabel('$$x$$', 'Interpreter', 'latex');
ylabel('$$V(x)$$','Interpreter', 'latex');
grid on;
hold off;
Solving and Plotting the Duffing Equation
% Define the system of ODEs for the non-chaotic Duffing equation
duffing_ode = @(t, X) [X(2);
X(1) - X(1).^3];</p><p>% Time span for the simulation
tspan = [0 10];</p><p>% Initial conditions [x(0), v(0)]
initial_conditions = [1; 1];</p><p>% Solve the ODE using ode45
[t, X] = ode45(duffing_ode, tspan, initial_conditions);</p><p>% Extract displacement (x) and velocity (v)
x = X(:, 1);
v = X(:, 2);</p><p>% Plot both x(t) and v(t) in the same figure
figure;
plot(t, x, 'b-', 'LineWidth', 2); % Plot x(t) with blue line
hold on;
plot(t, v, 'r--', 'LineWidth', 2); % Plot v(t) with red dashed line</p><p>% Add title, labels, and legend
title(' Component curve solutions to $$\ddot{x}-x+x^3=0$$','Interpreter', 'latex');
xlabel('t','Interpreter', 'latex');
ylabel('$$x(t) $$ and $$v(t) $$','Interpreter', 'latex');
legend('$$x(t)$$', ' $$v(t)$$','Interpreter', 'latex');
grid on;
hold off;</p><p>% Phase portrait with nullclines, equilibria, and vector field
figure;
hold on;</p><p>% Plot phase portrait
plot(x, v,'r', 'LineWidth', 2);</p><p>% Plot equilibrium points
plot([0 1 -1], [0 0 0], 'ro', 'MarkerSize', 5, 'MarkerFaceColor', 'g');</p><p>% Create a grid of points for the vector field
[x_vals, v_vals] = meshgrid(linspace(-2, 2, 20), linspace(-1, 1, 20));</p><p>% Compute the vector field components
dxdt = v_vals;
dvdt = x_vals - x_vals.^3;</p><p>% Plot the vector field
quiver(x_vals, v_vals, dxdt, dvdt, 'b');</p><p>% Set axis limits to [-1, 1]
xlim([-1.7 1.7]);
ylim([-1 1]);</p><p>% Labels and title
title('Phase-Plane solutions to $$\ddot{x}-x+x^3=0$$','Interpreter', 'latex');
xlabel('$$ (x)$$','Interpreter', 'latex');
ylabel('$$v(v)$$','Interpreter', 'latex');
grid on;
hold off;</p>Athanasios Paraskevopouloshttps://www.mathworks.com/matlabcentral/profile/authors/30623616