/matlabcentral/discussions/channels
Discussions Channels
2024-10-10T23:29:42Z
tag:fr.mathworks.com,2005:Topic/864841
2024-06-03T21:22:40Z
2024-10-01T10:01:07Z
Welcome 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 Lin
https://fr.mathworks.com/matlabcentral/profile/authors/6682740
tag:fr.mathworks.com,2005:Topic/872381
2024-07-31T20:34:15Z
2024-09-23T07:43:17Z
Using 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'Errico
https://fr.mathworks.com/matlabcentral/profile/authors/869215
tag:fr.mathworks.com,2005:Topic/844101
2024-02-02T17:41:51Z
2024-07-15T15:17:31Z
Read 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>
David
https://fr.mathworks.com/matlabcentral/profile/authors/4480925
tag:fr.mathworks.com,2005:Topic/876047
2024-10-09T18:54:26Z
2024-10-10T23:29:42Z
What would you ask the MATLAB leadership team?
<p>Let's say you have a chance to ask the MATLAB leadership team any question. What would you ask them?</p>
Hans Scharler
https://fr.mathworks.com/matlabcentral/profile/authors/5863695
tag:fr.mathworks.com,2005:Topic/875846
2024-10-02T15:17:52Z
2024-10-10T18:30:42Z
Discussions Update: A Page Redesign to Focus on Users
<p>We are thrilled to announce the redesign of the Discussions leaf page, with a new user-focused right-hand column!</p><p>Why Are We Doing This?
Address Readersâ€™ Needs:
Previously, the right-hand column displayed related content, but feedback from our community indicated that this wasn't meeting your needs. Many of you expressed a desire to read more posts from the same author but found it challenging to locate them.
With the new design, readers can easily learn more about the author, explore their other posts, and follow them to receive notifications on new content.</p><p>Enhance Authorsâ€™ Experience:
Since the launch of the Discussions area earlier this year, we've seen an influx of community members sharing insightful technical articles, use cases, and ideas. The new design aims to help you grow your followers and organize your content more effectively by editing tags. We highly encourage you to use the Discussions area as your community blogging platform.</p><p>We hope you enjoy the new design of the right-hand column. Please feel free to share your thoughts and experiences by leaving a comment below.</p>
Chen Lin
https://fr.mathworks.com/matlabcentral/profile/authors/6682740
tag:fr.mathworks.com,2005:Topic/875491
2024-09-13T13:10:42Z
2024-10-10T15:37:39Z
Answers not displaying symbolic results?
<p>syms u v
atan2alt(v,u)
function Z = atan2alt(V,U)
% extension of atan2(V,U) into the complex plane
Z = -1i*log((U+1i*V)./sqrt(U.^2+V.^2));
% check for purely real input. if so, zero out the imaginary part.
realInputs = (imag(U) == 0) & (imag(V) == 0);
Z(realInputs) = real(Z(realInputs));
end
As I am editing this post, I see the expected symbolic display in the nice form as have grown to love. However, when I save the post, it does not display. (In fact, it shows up here in the discussions post.) This seems to be a new problem, as I have not seen that failure mode in the past.
You can see the problem in this Answer forum response of mine, where it did fail.
https://www.mathworks.com/matlabcentral/answers/2152250-atan2-does-not-accept-complex-numbers-as-input?s_tid=srchtitle</p>
John D'Errico
https://fr.mathworks.com/matlabcentral/profile/authors/869215
tag:fr.mathworks.com,2005:Topic/875916
2024-10-06T10:47:28Z
2024-10-10T15:33:37Z
Paint store color swatch for Matlab
<p>If I go to a paint store, I can get foldout color charts/swatches for every brand of paint. I can make a selection and it will tell me the exact proportions of each of base color to add to a can of white paint. There doesn't seem to be any equivalent in MATLAB. The common word "swatch" doesn't even exist in the documentation. (?) One thinks pcolor() would be the way to go about this, but pcolor documentation is the most abstruse in all of the documentation. Thanks 1e+06 !</p>
Eric LePage
https://fr.mathworks.com/matlabcentral/profile/authors/2026298
tag:fr.mathworks.com,2005:Topic/876044
2024-10-09T17:34:24Z
2024-10-09T17:34:24Z
New feature Announcement - Create a Poll in Discussions
<p>We are thrilled to announce that every community member now has the ability to create a poll in Discussions, allowing you to gather votes and opinions from the community.
How to create a poll:
You can find the â€˜Create a Pollâ€™ link just below the text box (see screenshot below). Please note that the default type of content is a â€˜Discussionâ€™. To start a poll, simply click the link.</p><p>Creating a poll is straightforward. You can add up to 6 choices for your poll and set the duration from 1 to 6 weeks.</p><p>Where to find the poll
Polls created by community members will appear only in the channel where they are created and the landing page of Discussions area. Discussions moderators have the privilege to feature/broadcast the poll across Answers, File Exchange, and Cody.</p><p>Thoughts?
We canâ€™t wait to see what interesting polls our community will create. Meanwhile, if you have any questions or suggestions, feel free to leave a comment.</p>
Chen Lin
https://fr.mathworks.com/matlabcentral/profile/authors/6682740
tag:fr.mathworks.com,2005:Topic/875876
2024-10-03T17:25:50Z
2024-10-09T12:37:55Z
MATLAB EXPO 2024 is coming November 13â€“14 Online: Sign up for free
<p>If you are interested in AI, Autonomous Systems and Robotics, and the future of engineering, don't miss out on MATLAB EXPO 2024 and register now.
You will have the opportunity to connect with engineers, scientists, educators, and researchers, and new ideas.
Featured Sessions:
From Embedded to Empowered: The Rise of Software-Defined Products - MarÃa Elena GavilÃ¡n Alfonso, MathWorks
The Empathetic Engineers of Tomorrow - Dr. Darryll Pines, University of Maryland
A Model-Based Design Journey from Aerospace to an Artificial Pancreas System - Louis Lintereur, Medtronic Diabetes
Featured Topics:
AI
Autonomous Systems and Robotics
Electrification
Algorithm Development and Data Analysis
Modeling, Simulation, Verification, Validation, and Implementation
Wireless Communications
Cloud, Software Factories, and DevOps
Preparing Future Engineers and Scientists
Register now: https://www.matlabexpo.com/online/2024.html?s_eid=PSM_26443</p>
Toshiaki Takeuchi
https://fr.mathworks.com/matlabcentral/profile/authors/951521
tag:fr.mathworks.com,2005:Topic/842991
2024-01-26T14:27:19Z
2024-10-08T14:27:14Z
Multiple AIs in the chat playground
<p>When I want to understand a problem, I'll often use different sources. I'll read different textbooks, blog posts, research papers and ask the same question to different people. The differences in the solutions are almost always illuminating.
I feel the same way about AIs. Sometimes, I don't want to ask <b>THE</b> AI...I want to ask a bunch of them. They'll have different strengths and weaknesses..different personalities if you want to think of it that way.
I've been playing with the AI chat arena and there really is a lot of difference between the answers returned by different models. https://lmarena.ai/?arena
I think it would be great if the MATLAB Chat playgroundwere to allow the user to change which AI they were talking with.
What does everyone else think?</p>
Mike Croucher
https://fr.mathworks.com/matlabcentral/profile/authors/20789457
tag:fr.mathworks.com,2005:Topic/871716
2024-07-29T00:58:13Z
2024-10-07T21:49:43Z
writing matlab code using llm
<p>hello i found the following tools helpful to write matlab programs. copilot.microsoft.com chatgpt.com/gpts gemini.google.com and ai.meta.com. thanks a lot and best wishes.</p>
supercomputers
https://fr.mathworks.com/matlabcentral/profile/authors/34527997
tag:fr.mathworks.com,2005:Topic/848041
2020-07-31T15:16:44Z
2024-10-07T18:52:03Z
What is missing from MATLAB #2 - the next decade edition
<p></p>
Rik
https://fr.mathworks.com/matlabcentral/profile/authors/3073010
tag:fr.mathworks.com,2005:Topic/788054
2023-05-25T21:17:55Z
2024-10-07T18:43:56Z
Today, while using MATLAB, I'm going to....
Adam Danz
https://fr.mathworks.com/matlabcentral/profile/authors/25613423
tag:fr.mathworks.com,2005:Topic/875730
2024-09-25T20:12:04Z
2024-10-07T18:28:30Z
AI Chat Playground Upgrades: OpenAI GPT-4o mini and MATLAB R2024b
<p>The AI Chat Playground at MATLAB Central has two new upgrades: OpenAI GPT-4o mini and MATLAB R2024b!
https://www.mathworks.com/matlabcentral/playground/
GPT-4o mini is a new language model from OpenAI and brings general knowledge up to October 2023. GPT-4o mini surpasses GPT-3.5 Turbo and other small models on academic benchmarks across both textual intelligence and reasoning. Our goal is to keep improving the output of the AI Chat Playground. This upgrade is available now: https://www.mathworks.com/matlabcentral/playground/
One more thing... we also updated the system to the latest release of MATLAB. This is R2024b and comes with hundreds of updates and new plot types to explore.Check out Mike Croucher's blog post about the latest version of MATLAB: https://blogs.mathworks.com/matlab/2024/09/13/the-latest-version-of-matlab-r2024b-has-just-been-released/
We are looking forward to your feedback on the updates to the AI Chat Playground. Let us know what you think and how you use this community app.</p>
Hans Scharler
https://fr.mathworks.com/matlabcentral/profile/authors/5863695
tag:fr.mathworks.com,2005:Topic/875976
2024-10-07T17:25:33Z
2024-10-07T17:25:33Z
Tools Opinion
<p>hello i found the following tools helpful to write matlab programs. copilot.microsoft.com chatgpt.com/gpts gemini.google.com and ai.meta.com. thanks a lot and best wishes.</p>
saket singh
https://fr.mathworks.com/matlabcentral/profile/authors/25521730
tag:fr.mathworks.com,2005:Topic/857641
2024-04-27T21:03:01Z
2024-10-07T09:07:58Z
How many spaces per tab do you prefer?
goc3
https://fr.mathworks.com/matlabcentral/profile/authors/5349647
tag:fr.mathworks.com,2005:Topic/875944
2024-10-07T04:03:55Z
2024-10-07T04:03:55Z
2024 Community Contest Starts Today. Have Fun and Win Prizes!
<p>We're excited to announce that the 2024 Community Contestâ€”MATLAB Shorts Mini Hack starts today! The contest will run for 5 weeks, from Oct. 7th to Nov. 10th.</p><p>What creative short movies will you create? Let the party begin, and we look forward to seeing you all in the contest!</p>
Chen Lin
https://fr.mathworks.com/matlabcentral/profile/authors/6682740
tag:fr.mathworks.com,2005:Topic/875796
2024-09-30T14:20:04Z
2024-10-07T01:51:40Z
Mark Your Calendars: Community Contest 2024 Starts on October 7th!
<p>Dear contest participants,
The 2024 Community Contestâ€”MATLAB Shorts Mini Hackâ€”is just one week away! Last year, we challenged you to create a 48-frame, 2-second animation. This year, we're doubling the fun by increasing the frame count to 96 and adding audio support. Your mission? Create a short movie!
As always, whether you are a seasoned MATLAB user or just a beginner, you can participate in the contest and have opportunities to win amazing prizes.</p><p>Timeframe:
The contest will run for 5 weeks, from Oct. 7th to Nov. 10th, Eastern Time.
General Rules:
The first week is dedicated to entry creation, and the fifth week is reserved for voting only.
Create a 96-frame, 4-second animation and add audio. We will loop it 3 times to create a 12-second short movie for you.
The character limit remains at 2,000 characters.
Prizes
You will have opportunities to win compelling prizes, including Amazon gift cards, MathWorks T-shirts, and virtual badges. We will give out both weekly prizes and grand prizes.
Warm-up!
With one week left before the contest begins, we recommend you warm up by reading a fantastic article: Walkthrough: making Little Nemo's airship in MATLAB by @Tim. The article shares both technical insights and the challenges encountered along the way.</p><p>We look forward to seeing all of you in the 2024 MATLAB Shorts Mini Hack.</p><p>The MATLAB Central Community Team</p>
Chen Lin
https://fr.mathworks.com/matlabcentral/profile/authors/6682740
tag:fr.mathworks.com,2005:Topic/875587
2024-09-19T02:31:09Z
2024-10-05T07:03:21Z
Walkthrough: making Little Nemo's airship in Matlab
<p>In the spirit of warming up for this year's minihack contest, I'm uploading a walkthrough for how to design an airship using pure Matlab script. This is commented and uncondensed; half of the challenge for the minihacks is how minimize characters. But, maybe it will give people some ideas.
The actual airship design is from one of my favorite original NES games that I played when I was a kid - Little Nemo: The Dream Master. The design comes from the intro of the game when Nemo sees the Slumberland airship leave for Slumberland:</p><p>(Snip from a frame of the opening scene in Capcom's game Little Nemo: The Dream Master, showing the Slumberland airship).
I spent hours playing this game with my two sisters, when we were little. It's fun and tough, but the graphics sparked the imagination. On to the code walkthrough, beginning with the color palette: these four colors are the only colors used for the airship:
c1=cat(3,1,.7,.4); % Cream color
c2=cat(3,.7,.1,.3); % Magenta
c3=cat(3,0.7,.5,.1); % Gold
c4=cat(3,.5,.3,0); % bronze</p><p>We start with the airship carriage body. We want something rectangular but smoothed on the corners. To do this we are going to start with the separate derivatives of the x and y components, which can be expressed using separate blocks of only three levels: [1, 0, -1]. You could integrate to create a rectangle, but if we smooth the derivatives prior to integrating we will get rounded edges. This is done in the following code:
% Binary components for x & y vectors
z=zeros(1,30);
o=ones(1,100);</p><p>% X and y vectors
x=[z,o,z,-o];
y=[1+z,1-o,z-1,1-o];</p><p>% Smoother function (fourier / circular)
s=@(x)ifft(fft(x).*conj(fft(hann(45)'/22,260)));</p><p>% Integrator function with replication and smoothing to form mesh matrices
u=@(x)repmat(cumsum(s(x)),[30,1]);</p><p>% Construct x and y components of carriage with offsets
x3=u(x)-49.35;
y3=u(y)+6.35;
y3 = y3*1.25; % Make it a little fatter</p><p>% Add a z-component to make the full set of matrices for creating a 3D
% surface:
z3=linspace(0,1,30)'.*ones(1,260)*30;
z3(14,:)=z3(15,:); % These two lines are for adding platforms
z3(2,:)=z3(3,:); % to the carriage, later.
Plotting x, y, and the top row of the smoothed, integrated, and replicated matrices x3 and y3 gives the following:</p><p>We now have the x and y components for a 3D mesh of the carriage, let's make it more interesting by adding a color scheme including doors, and texture for the trim around the doors. Let's also add platforms beneath the doors for passengers to walk on.
Starting with the color values, let's make doors by convolving points in a color-matrix by a door shaped function.
m=0*z3; % Image matrix that will be overlayed on carriage surface
m(7,10:12:end)=1; % Door locations (lower deck)
m(21,10:12:end)=1; % Door locations (upper deck)
drs = ones(9, 5); % Door shape
m=1-conv2(m,ones(9,5),'same'); % Applying
To add the trim, we will convolve matrix "m" (the color matrix) with a square function, and look for values that lie between the extrema. We will use this to create a displacement function that bumps out the -x, and -y values of the carriage surface in intermediary polar coordinate format.
rm=conv2(m,ones(5)/25,'same'); % Smoothing the door function
rm(~m)=0; % Preserving only the region around the doors</p><p>rds=0*m; % Radial displacement function
rds(rm<1&rm>0)=1; % Preserving region around doors
rds(m==0)=0;
rds(13:14,:)=6; % Adding walkways
rds(1:2,:)=6;</p><p>% Apply radial displacement function
[th,rd]=cart2pol(x3,y3);
[x3T,y3T]=pol2cart(th,(rd+rds)*.89);
If we plot the color function (m) and radial displacement function (rds) we get the following:</p><p>In the upper plot you can see the doors, and in the bottom map you can see the walk way and door trim.
Next, we are going to add some flags draped from the bottom and top of the carriage. We are going to recycle the values in "z3" to do this, by multiplying that matrix with the absolute value of a sine-wave, squished a bit with the soft-clip erf() function.
We add a keel to the airship carriage using a canonical sphere turned on its side, again using the soft-clip erf() function to make it roughly rectangular in x and y, and multiplying with a vector that is half nan's to make the top half transparent.
At this point, since we are beginning the plotting of the ship, we also need to create our hgtransform objects. These allow us to move all of the components of the airship in unison, and also link objects with pivot points to the airship, such as the propeller.
% Now we need some flags extending around the top and bottom of the
% carriage. We can do this my multiplying the height function (z3) with the
% absolute value of a sine-wave, rounded with a compression function
% (erf() in this case);
g=-z3.*erf(abs(sin(linspace(0,40*pi,260))))/4; % Flags</p><p>% Also going to add a slight taper to the carriage... gives it a nice look
tp=linspace(1.05,1,30)';</p><p>% Finally, plotting. Plot the carriage with the color-map for the doors in
% the cream color, than the flags in magenta. Attach them both to transform
% objects for movement.</p><p>% Set up transform objects. 2 moving parts:
% 1) The airship itself and all sub-components
% 2) The propellor, which attaches to the airship and spins on its axis.
hold on;
P=hgtransform('Parent',gca); % Ship
S=hgtransform('Parent',P); % Prop</p><p>surf(x3T.*tp,y3T,z3,c1.*m,'Parent',P);
surf(x3,y3,g,c2.*rd./rd, 'Parent', P);
surf(x3,y3,g+31,c2.*rd./rd, 'Parent', P);
axis equal</p><p>% Now add the keel of the airship. Will use a canonical sphere and the
% erf() compression function to square off.
[x,y,z]=sphere(99);
mk=round(linspace(-1,1).^2+.3); % This function makes the top half of the sphere nan's for transparency.
surf(50*erf(1.4*z),15*erf(1.4*y),13*x.*mk./mk-1,.5*c2.*z./z, 'Parent', P);</p><p>% The carriage is done. Now we can make the blimp above it.
We haven't adjusted the shading of the image yet, but you can see the design features that have been created:</p><p>Next, we start working on the blimp. This is going to use a few more vertices & faces. We are going to use a tapered cylinder for this part, and will start by making the overlaid image, which will have 2 colors plus radial rings, circles, and squiggles for ornamentation.
M=525; % Blimp (matrix dimensions)
N=700;</p><p>% Assign the blimp the cream and magenta colors
t=122; % Transition point
b=ones(M,N,3); % Blimp color map template
bc=b.*c1; % Blimp color map
bc(:,t+1:end-t,:)=b(:,t+1:end-t,:).*c2;</p><p>% Add axial rings around blimp
l=[.17,.3,.31,.49];
l=round([l,1-fliplr(l)]*N); % Mirroring
lnw=ones(1,N); % Mask
lnw(l)=0;
lnw=rescale(conv(lnw,hann(7)','same'));
bc=bc.*lnw;</p><p>% Now add squiggles. We're going to do this by making an even function in
% the x-dimension (N, 725) added with a sinusoidal oscillation in the
% y-dimension (M, 500), then thresholding.</p><p>r=sin(linspace(0, 2*pi, M)*10)'+(linspace(-1, 1, N).^6-.18)*15;
q=abs(r)>.15;
r=sin(linspace(0, 2*pi, M)*12)'+(abs(linspace(-1, 1, N))-.25)*15;
q=q.*(abs(r)>.15);</p><p>% Now add the circles on the blimp. These will be spaced evenly in the
% polar angle dimension around the axis. We will have 9. To make the
% circles, we will create a cone function with a peak at the center of the
% circle, and use thresholding to create a ring of appropriate radius.</p><p>hs=[1,.75,.5,.25,0,-.25,-.5,-.75,-1]; % Axial spacing of rings</p><p>% Cone generation and ring loop
xy= @(h,s)meshgrid(linspace(-1, 1, N)+s*.53,(linspace(-1, 1, M)+h)*1.15);
w=@(x,y)sqrt(x.^2+y.^2);
for n=1:9
h=hs(n);
[xx,yy]=xy(h,-1);
r1=w(xx,yy);
[xx,yy]=xy(h,1);
r2=w(xx,yy);
b=@(x,y)abs(y-x)>.005;
q=q.*b(.1,r1).*b(.075,r1).*b(.1,r2).*b(.075,r2);
end
The figures below show the color scheme and mask used to apply the squiggles and circles generated in the code above:</p><p>Finally, for the colormap we are going to smooth the binary mask to avoid hard transitions, and use it to to add a "puffy" texture to the blimp shape. This will be done by diffusing the mask iteratively in a loop with a non-linear min() operator.
% 2D convolution function
ff=@(x)circshift(ifft2(fft2(x).*conj(fft2(hann(7)*hann(7)'/9,M,N))),[3,3]);
q=ff(q); % Smooth our mask function
hh=rgb2hsv(q.*bc); % Convert to hsv: we are going to use the value
% component for radial displacement offsets of the
% 3D blimp structure.
rd=hh(:,:,3); % Value component
for n=1:10
rd=min(rd,ff(rd)); % Diffusing the value component for a puffy look
end
rd=(rd+35)/36; % Make displacements effects small</p><p>% Now make 3D blimp manifold using "cylinder" canonical shape
[x,y,z]=cylinder(erf(sin(linspace(0,pi,N)).^.5)/4,M-1); % First argument is the blimp taper
[t,r]=cart2pol(x, y);
[x2,y2]=pol2cart(t, r.*rd'); % Applying radial displacment from mask
s=200;</p><p>% Plotting the blimp
surf(z'*s-s/2, y2'*s, x2'*s+s/3.9+15, q.*bc,'Parent',P);
Notice that the parent of the blimp surface plot is the same as the carriage (e.g. hgtransform object "P"). Plotting at this point using flat shading and adding some lighting gives the image below:</p><p>Next, we need to add a propeller so it can move. This will include the creation of a shaft using the cylinder() function. The rest of the pieces (the propeller blades, collars and shaft tip) all use the same canonical sphere with distortions applied using various math functions. Note that when the propeller is made it is linked to hgtransform object "S" rather than "P." This will allow the propeller to rotate, but still be joined to the airship.
% Next, the propeller. First, we start with the shaft. This is a simple
% cylinder. We add an offset variable and a scale variable to move our
% propeller components around, as well.</p><p>shx = -70; % This is our x-shifter for components
scl = 3; % Component size scaler</p><p>[x,y,z]=cylinder(1, 20); % Canonical cylinder for prop shaft.
p(1)=surf(-scl*(z-1)*7+shx,scl*x/2,scl*y/2,0*x+c4,'Parent',P); % Prop shaft</p><p>% Now the propeller. This is going to be made from a distorted sphere.
% The important thing here is that it is linked to the "S" hgtransform
% object, which will allow it to rotate.</p><p>[x,y,z]=sphere(50);
a=(-1:.04:1)';
x2=(x.*cos(a)-y/3.*sin(a)).*(abs(sin(a*2))*2+.1);
y2=(x.*sin(a)+y/3.*cos(a));
p(2)=surf(-scl*y2+shx,scl*x2,scl*z*6,0*x+c3,'Parent',S);</p><p>% Now for the prop-collars. You can see these on the shaft in the NES
% animation. These will just be made by using the canonical sphere and the
% erf() activation function to square it in the x-dimension.
g=erf(z*3)/3;
r=@(g)surf(-scl*g+shx,scl*x,scl*y,0*x+c3,'Parent',P);
r(g);
r(g-2.8);
r(g-3.7);</p><p>% Finally, the prop shaft tip. This will just be the sphere with a
% taper-function applied radially.
t=1.7*cos(0:.026:1.3)'.^2;
p(3)=surf(-(z*2+2)*scl + shx,x.*t*scl,y.*t*scl, 0*x+c4,'Parent',P);</p><p>Now for some final details including the ropes to the blimp, a flag hung on one of the ropes, and railings around the walkways so that passengers don't plummet to their doom. This will make use of the ad-hoc "ropeG" function, which takes a 3D vector of points and makes a conforming cylinder around it, so that you get lighting functions etc. that don't work on simple lines. This function is added to the script at the end to do this:
% Rope function for making a 3D curve have thickness, like a rope.
% Inputs:
% - xyz (3D curve vector, M points in 3 x M format)
% - N (Number of radial points in cylinder function around the curve
% - W (Width of the rope)
%
% Outputs:
% - xf, yf, zf (Matrices that can be used with surf())</p><p>function [xf, yf, zf] = RopeG(xyz, N, W)</p><pre> % Canonical cylinder with N points in circumference
[xt,yt,zt] = cylinder(1, N);</pre><pre> % Extract just the first ring and make (W) wide
xyzt = [xt(1, :); yt(1, :); zt(1, :)]*W;</pre><pre> % Get local orientation vector between adjacent points in rope
dxyz = xyz(:, 2:end) - xyz(:, 1:end-1);
dxyz(:, end+1) = dxyz(:, end);
vcs = dxyz./vecnorm(dxyz);</pre><pre> % We need to orient circle so that its plane normal is parallel to
% xyzt. This is a kludgey way to do that.
vcs2 = [ones(2, size(vcs, 2)); -(vcs(1, :) + vcs(2, :))./(vcs(3, :)+0.01)];
vcs2 = vcs2./vecnorm(vcs2);
vcs3 = cross(vcs, vcs2);
p = @(x)permute(x, [1, 3, 2]);
rmats = [p(vcs3), p(vcs2), p(vcs)];</pre><pre> % Create surface
xyzF = pagemtimes(rmats, xyzt) + permute(xyz, [1, 3, 2]);</pre><pre> % Outputs for surf format
xf = squeeze(xyzF(1, :, :));
yf = squeeze(xyzF(2, :, :));
zf = squeeze(xyzF(3, :, :));</pre><p>end
Using this function we can define the ropes and balconies. Note that the balconies simply recycle one of the rows of the original carriage surface, defining the outer rim of the walkway, but bumping up in the z-dimension.
cb=-sqrt(1-linspace(1, 0, 100).^2)';
c1v=[linspace(-67, -51)', 0*ones(100,1),cb*30+35];
c2v=[c1v(:,1),c1v(:,2),(linspace(1,0).^1.5-1)'*15+33];
c3v=c2v.*[-1,1,1];
[xr,yr,zr]=RopeG(c1v', 10, .5);
surf(xr,yr,zr,0*xr+c2,'Parent',P);
[xr,yr,zr]=RopeG(c2v', 10, .5);
surf(xr,yr,zr,0*zr+c2,'Parent',P);
[xr,yr,zr]=RopeG(c3v', 10, .5);
surf(xr,yr,zr,0*zr+c2,'Parent',P);</p><p>% Finally, balconies would add a nice touch to the carriage keep people
% from falling to their death at 10,000 feet.
[rx,ry,rz]=RopeG([x3T(14, :); y3T(14,:); 0*x3T(14,:)+18]*1.01, 10, 1);
surf(rx,ry,rz,0*rz+cat(3,0.7,.5,.1),'Parent',P);
surf(rx,ry,rz-13,0*rz+cat(3,0.7,.5,.1),'Parent',P);
And, very last, we are going to add a flag attached to the outer cable. Let's make it flap in the wind. To make it we will recycle the z3 matrix again, but taper it based on its x-value. Then we will sinusoidally oscillate the flag in the y dimension as a function of x, constraining the y-position to be zero where it meets the cable. Lastly, we will displace it quadratically in the x-dimension as a function of z so that it lines up nicely with the rope. The phase of the sine-function is modified in the animation loop to give it a flapping motion.
h=linspace(0,1);
sc=10;
[fx,fz]=meshgrid(h,h-.5);
F=surf(sc*2.5*fx-90-2*(fz+.5).^2,sc*.3*erf(3*(1-h)).*sin(10*fx+n/5),sc*fz.*h+25,0*fx+c3,'Parent',P);
Plotting just the cables and flag shows:</p><p>Putting all the pieces together reveals the full airship:</p><p>A note about lighting: lighting and material properties really change the feel of the image you create. The above picture is rendered in a cartoony style by setting the specular exponent to a very low value (1), and adding lots of diffuse and ambient reflectivity as well. A light below the airship was also added, albeit with lower strength. Settings used for this plot were:
shading flat
view([0, 0]);
L=light;
L.Color = [1,1,1]/4;
light('position', [0, 0.5, 1], 'color', [1,1,1]);
light('position', [0, 1, -1], 'color', [1, 1, 1]/5);
material([1, 1, .7, 1])
set(gcf, 'color', 'k');
axis equal off
What about all the rest of the stuff (clouds, moon, atmospheric haze etc.) These were all (mostly) recycled bits from previous minihack entries. The clouds were made using power-law noise as explained in Adam Danz' blog post. The moon was borrowed from moonrun, but with an increased number of points. Atmospheric haze was recycled from Matlon5. The rest is just camera angles, hgtransform matrix updates, and updating alpha-maps or vertex coordinates.
Finally, the use of hann() adds the signal processing toolbox as a dependency. To avoid this use the following anonymous function:
hann = @(x)-cospi(linspace(0,2,x)')/2+.5;</p>
Tim
https://fr.mathworks.com/matlabcentral/profile/authors/4596244
tag:fr.mathworks.com,2005:Topic/847991
2019-11-23T15:00:20Z
2024-10-04T19:31:02Z
Table of Matlab release features
<p></p>
Jan
https://fr.mathworks.com/matlabcentral/profile/authors/869888