I am learning to generate meshes in Octave for a given geometric domain. Unlike MATLAB, where 'initmesh' requires a geometric domain 'g', I am unable to find such type of function in Octave.
I am working with 'delaunay', which is great for Rectangular type domains. It would be really helpful if you could demonstrate meshing in L domain.
Two options are the msh package/gmsh or distmesh.
An example of msh is here. You need to make sure that you have gmsh available in octave to get that working.
Using distmesh, I created a mesh for an L domain using this command:
>> pv = [0 0; 3 0; 3 1; 1 1; 1 4; 0 4; 0 0];
>> [p,t]=distmesh2d(#dpoly,#huniform,0.2,[-1,-1; 5,5],pv,pv);
distmesh involves some c++ code and thus platform dependent libraries. There is a slower, pure matlab variant here where you can the matlab implementation of the dsegment function for this.
Related
Is there any method to find the root of a polynomial, not in matrix form, in MATLAB?
I know, to find roots of a polynomial (say, p(x) = x^.2 - 4), I should do the following:
p = [1 0 -4];
r = roots(p)
What I wanted to know if there is some way to find the root of a function (say p(x) = x^.2 - 4) already present in polynomial form (not in matrix form) in my matlab code? Like anything similar to r = roots(p(x)) (this doesn't work, of course).
Root is good
First of all the solution using roots is probably the one that will give you the most accurate and fastest results if you are indeed working with polynomials. I will acknowledge that it might be an issue if your function is not a polynomial.
Finding the roots of a function
If you don't want to use roots that means you will probably have to represent your polynomial as an anonymous function. Then you can use any root-finding algorithm on that function. Wikipedia has a few of them listed. What is tricky is that in general they don't guarantee that they will find one root, let alone all of them. So you might need as much prior information on your function as you can.
In matlab you can use fzero. The issue with it is that it only finds one zero and that it will only find zeros where the function changes sign (it wouldn't work on p(x) = x² for example). This is how you would implement it:
p = #(x) x.^2 - 4; % Define your polynomial as an anonymous function
x0 = 12; % Initial guess for the zero
% Find a root
fzero(p, x0)
>>> ans = 2
% Now with a different initial guess for a different solution
x0 = -12;
fzero(p, x0)
>>> ans = -2
As you can see this works only if you want to find a root and don't care which one it is.
Problem
The issue is that you polynomials with integer or rational coefficients have a way of finding the roots by using square-free factorization. Yet you can only apply that if you have some way of storing and accessing those coefficients in matlab. The anonymous functions don't allow you to do that. That's why roots works with a matrix and not an anonymous function.
I am trying to integrate x^3/(exp(x)-1) in the limit 0 to infinity with respect to x,and it should answer pi^4/15 but it instead of this ocatve is printing original integral in symbolic form. How to resolve this issue? I tried same integral on MATLAB mobile and it is giving correct pi^4/15
First of all:
pi^4 / 15 = 6.4939
I tried the below code in octave online
fun = #(x) (x.^3)./(exp(x)-1);
q = integral(fun, 0, Inf)
The answer is:
q = 6.4939
The following is example code from Matlab. It doesn't run in Octave. The code is:
f = figure;
b = uicontrol(f,'Style','pushbutton');
b.Position = [100 100 50 20];
It is from the online documentation: https://www.mathworks.com/help/matlab/ref/matlab.ui.control.uicontrol-properties.html
In Octave, I get: error: scalar cannot be indexed with .
What change must be made to make this run in Octave?
MATLAB introduced the second version of the handle graphics system (HG2) a couple of years ago. Octave still uses the old system.
Every time you see handle.propery, you are dealing with HG2. In the original system, we used get(handle,'property') and set(handle,'property',newvalue). Note that MATLAB will not deprecate this original syntax any time soon, it is perfectly valid to use both forms with the newer versions of MATLAB. Thus, the set and get functions are to be preferred for compatibility reasons.
So you can replace
b.Position = [100 100 50 20];
with
set(b,'Position',[100 100 50 20]);
Started learning octave recently. How do I generate a matrix from another matrix by applying a function to each element?
eg:
Apply 2x+1 or 2x/(x^2+1) or 1/x+3 to a 3x5 matrix A.
The result should be a 3x5 matrix with the values now 2x+1
if A(1,1)=1 then after the operation with output matrix B then
B(1,1) = 2.1+1 = 3
My main concern is a function that uses the value of x like that of finding the inverse or something as indicated above.
regards.
You can try
B = A.*2 + 1
The operator . means application of the following operation * to each element of the matrix.
You will find a lot of documentation for Octave in the distribution package and on the Web. Even better, you can usually also use the extensive documentation on Matlab.
ADDED. For more complex operations you can use arrayfun(), e.g.
B = arrayfun(#(x) 2*x/(x^2+1), A)
when we tried to implement a program with one for loop with a limit of 100,octave failed to show all the results.this is the problem with buffer. how can we overcome that?
Try to express the problem in terms of matrices. MATLAB and Octave are optimized for matrix operations. Here is an excerpt of what the MATLAB documentation site says about vectorizing loops:
The MATLAB software uses a matrix language, which means it is designed for vector and matrix operations. You can often speed up your code by using vectorizing algorithms that take advantage of this design. Vectorization means converting for and while loops to equivalent vector or matrix operations.
They also provide a simple example of vectorizing a loop to compute the sine of 1001 values ranging from 0 to 10:
i = 0;
for t = 0:.01:10
i = i + 1;
y(i) = sin(t);
end
To a vectorized version of the same code:
t = 0:.01:10;
y = sin(t);
There are more details in the MATLAB Code Vectorization Guide
and some examples in these few related questions:
Loopless function calls on vector/matrix members in Matlab/Octave
Matlab - Speeding up a Nested For Loop