How can I specify different entires in a vector - partially using a function
Ältere Kommentare anzeigen
Hi!
I'm trying to produce the initial conditions (using a vector) for a differential equation where the first and last set of entries are 0, the middle is nonzero. The function is:
IC(x) = (x-x0)^2 (x-x1)^2 for x in (x0,x1) = 0 elsewhere
I'm thinking I can define the vector as IC(x), then redefine the rest of the vector entries part by part as zeros. In other words, if the input is [0,1] with delta x = 0.1 and x0 = 1/3, x1 = 2/3, then
IC(0) = IC(.1) = IC(.2) = IC(.3) = 0
IC(.4) = (.4-x0)^2 (.4-x1)^2 IC(.5) = (.5-x0)^2 (.5-x1)^2 IC(.6) = (.6-x0)^2 (.6-x1)^2
IC(.7) = IC(.8) = IC(.9) = IC(1) = 0
This is the code I have written so far. (The variables a & b are there so I don't get a 'dimensions don't agree' error) I can define the function, but I don't know how to modify the first and last set of entires so that they are zero. Any input will be very much appreciated!
Thank you!
E
function[IC] = Initial_Condition(L, Delta_x)
x = 0:Delta_x:L; n = length(x); x0 = floor(n/3)*Delta_x; x1 = floor(2*n/3)*Delta_x;
first_I = 0 : Delta_x : x0; second_I = x0 + Delta_x : Delta_x : x1; third_I = x1 + Delta_x : Delta_x : L;
IC = zeros(1,n);
a = (x-x1).^2; b = (x-x0).^2; IC = a.*b; IC'
plot(x,IC,'rd')
end
Akzeptierte Antwort
Weitere Antworten (4)
dpb
am 4 Aug. 2013
function y=initial_cond(x,x0,x1)
% define function f(x)=(x-x0)^2 * (x-x1)^2
% over range x0 <= x <= x1;
% f(x) = 0 otherwise.
% INPUT
% X -- vector of x to be evaluated
% X0,X1-clipping range
% OUTPUT
% Y -- vector of length(X) of
% f(x) = (x-x0)^2 * (x-x1)^2, x0 <= x <= x1
% = 0, otherwise
y=zeros(size(x));
ix=iswithin(x,x0,x1);
y(ix)=(x(ix)-x0).^2.*(x(ix)-x1).^2;
This is another use for my utility function iswithin
function flg=iswithin(x,lo,hi)
% returns T for values within range of input
% SYNTAX:
% [log] = iswithin(x,lo,hi)
% returns T for x between lo and hi values, inclusive
flg= (x>=lo) & (x<=hi);
NB: I put the onus of creating the overall x vector and it's spacing to the caller--only the bounds are needed in the function. If you want to pass the start/stop/dx as well, that's a trivial modification obviously.
dpb
am 4 Aug. 2013
For the specific inputs so you don't have to think any...
function y = ICondition(L, dx)
x = 0:dx:L;
n = length(x);
x0 = x(floor(n/3));
x1 = x(floor(2*n/3));
ix=iswithin(x,x0,x1);
y=zeros(size(x));
y(ix)=(x(ix)-x0).^2.*(x(ix)-x1).^2;
QED
3 Kommentare
Eric Johnson
am 4 Aug. 2013
dpb
am 4 Aug. 2013
If you're really going to do it this w/, you might find round better than floor--unless you must have the two intervals <= the value. round will give the closer numerical difference.
Eric Johnson
am 4 Aug. 2013
Eric Johnson
am 4 Aug. 2013
0 Stimmen
4 Kommentare
dpb
am 4 Aug. 2013
Well, mustn't have tried too hard... :(
Eric Johnson
am 4 Aug. 2013
dpb
am 4 Aug. 2013
>> x=[0:0.01:1];
>> x0=1/3;x1=2/3;
>> y=initial_cond(x,x0,x1);
>> plot(x,y)
>> % your original variables instead...
>>
>> L=1;
>> dx=0.01;
>> x=0:dx:L;
>> y=initial_cond(x,x0,x1);
>> plot(x,y)
>>
>> % or even closer w/ no changes to the function...
>> y=initial_cond([0:dx:L],x0,x1);
>> plot(x,y)
>>
function y=initial_cond(x,x0,x1)
% define function f(x)=(x-x0)^2 * (x-x1)^2
% over range x0 <= x <= x1;
% f(x) = 0 otherwise.
% INPUT
% X -- vector of x to be evaluated
% X0,X1-clipping range
% OUTPUT
% Y -- vector of length(X) of
% f(x) = (x-x0)^2 * (x-x1)^2, x0 <= x <= x1
% = 0, otherwise
y=zeros(size(x));
ix=iswithin(x,x0,x1);
y(ix)=(x(ix)-x0).^2.*(x(ix)-x1).^2;
>>
dpb
am 4 Aug. 2013
Sorry if was a little snippy...yesterday had already been a long day at the time so wasn't exactly feeling in a particularly expansive mood... :)
When I had tested that code snippet, hearing that there was some perceived problem in it w/o seeing what had tried to cause the problem I reacted more strongly than perhaps warranted.
Apologies rendered....
Eric Johnson
am 4 Aug. 2013
0 Stimmen
2 Kommentare
dpb
am 4 Aug. 2013
Multiply the vector g element by element by this id_inRange vector applied to x.
That's what the logic addressing does transparently...you never showed where you had an error so can only guess but the code posted worked as is. I'd hypothesize the most likely problem is that you didn't use the logical address vector on both LHS and RHS so the effective lengths weren't the same of the target and the object. The above extra multiplication is simply the assignment
g(~idx)=0;
to zero the non-included elements.
The function the way I wrote it just computed the non-zero elements by selecting the subset of the range that is to be included.
Jan
am 4 Aug. 2013
@Eric: Please use the "{} Code" button to apply code formatting for a marked section of code. Then your code will be readable.
Kategorien
Mehr zu Get Started with MATLAB finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!