can anyone help me fix this? I want to create two conditions for K

% clc;
clear;
format long e
%untuk N=10
syms x
if i==0:.1:0.5
for k=0:10
for j=1:11
K(j,k+1)=int((x-[i(j)])*euler(k,x),x,[0,i(j)]);
E(j,k+1)=euler(k,[i(j)]);
F(j)=([i(j)]/2);
end
end
else i==0.6:.1:1
for k=0:10
for j=1:11
K(j,k+1)=int((x+[i(j)])*euler(k,x),x,[0,i(j)]);
E(j,k+1)=euler(k,[i(j)]);
F(j)=([i(j)]/2);
end
end
end
K=double(K);
E=double(E);
F=double(F);
F1=F.'
Ek=E-K;
Invers_Ek=inv(Ek);
C=Ek\F1 ;
%solusi aproximasinya
Ua=@(x)(C(1)*euler(0,x)+C(2)*euler(1,x)+C(3)*euler(2,x)+C(4)*euler(3,x)+C(5)*euler(4,x)+C(6)*euler(5,x)+C(7)*euler(6,x)+C(8)*euler(7,x)+C(9)*euler(8,x)+C(10)*euler(9,x)+C(11)*euler(10,x)) ;
Ue=@(x)((1/2)*sin(x)) ;
uaa=zeros(11,1) ;
uee=zeros(11,1) ;
xx=zeros(11,1) ;
k=0;
for i=1:11
uaa(i)=Ua(k);
uee(i)=Ue(k);
xx(i)=k;
k=k+.1;
end
y=(abs(uaa-uee));
[xx uee uaa y];
uee;
uaa;
y

댓글 수: 1

if i==0:.1:0.5
There are 6 different values on the right hand side of the "=="
for j=1:11
K(j,k+1)=int((x-[i(j)])*euler(k,x),x,[0,i(j)]);
but with j going to 11, up to i(11) will be requested
else i==0.6:.1:1
There are 5 different values on the right hand side of the "=="
for k=0:10
for j=1:11
K(j,k+1)=int((x+[i(j)])*euler(k,x),x,[0,i(j)]);
but with j going up to 11, up to i(11) will be requested.
Your code does not seem to show anything conditional on i -- you seem to write to all the same locations in both cases, just with a difference about whether i(j) is being added or subtracted from x.
Is it possible that your intention is like
syms ij
K(k+1) = int((x + ij .* piecewise(ij <= 1/2, -1, 1))*euler(k,x), x, 0, ij)
because if so then
syms x ij k
K_formula = int((x + ij .* piecewise(ij <= 1/2, -1, 1))*euler(k,x), x, 0, ij)
k_values = 0:11;
i_values = 0:.1:1;
K = subs(K_formula, {k, ij}, {k_values, i_values.'});

댓글을 달려면 로그인하십시오.

답변 (1개)

Relly Syam
Relly Syam 2021년 9월 2일
편집: Walter Roberson 2021년 9월 2일
%
I want to try run this
with different case i.e.
when 0<=i<=0.5 then K(j,k+1)=int((x-[i(j)])*euler(k,x),x, [0,i(j)]);
When 0.6<=i<=1 then K(j,k+1)=int((x+[i(j)])*euler(k,x),x,[0,i(j)]);
I'm still a beginner in using matlab so I hope you understand what I mean
clc;
clear;
format long e
%untuk N=10
syms x
i=0:.1:1;
for k=0:10
for j=1:11
K(j,k+1)=int((x-[i(j)])*euler(k,x),x,[0,i(j)]);
E(j,k+1)=euler(k,[i(j)]);
F(j)=([i(j)]/2);
end
end
K=double(K);
E=double(E);
F=double(F);
F1=F.'
Ek=E-K;
Invers_Ek=inv(Ek);
C=Ek\F1 ;
%solusi aproximasinya
Ua=@(x)(C(1)*euler(0,x)+C(2)*euler(1,x)+C(3)*euler(2,x)+C(4)*euler(3,x)+C(5)*euler(4,x)+C(6)*euler(5,x)+C(7)*euler(6,x)+C(8)*euler(7,x)+C(9)*euler(8,x)+C(10)*euler(9,x)+C(11)*euler(10,x)) ;
Ue=@(x)((1/2)*sin(x)) ;
uaa=zeros(11,1) ;
uee=zeros(11,1) ;
xx=zeros(11,1) ;
k=0;
for i=1:11
uaa(i)=Ua(k);
uee(i)=Ue(k);
xx(i)=k;
k=k+.1;
end
y=(abs(uaa-uee));
[xx uee uaa y];
uee
uaa
y

댓글 수: 3

format long e
syms x
ivals = 0:.1:1;
num_i = length(ivals);
for k=0:10
for j=1:num_i
i = ivals(j);
if 0 <= i & i <= 0.5
K(j,k+1) = int((x-i)*euler(k,x),x,[0,i]);
elseif 0.6 <= i & i <= 1
K(j,k+1)=int((x+i)*euler(k,x),x,[0,i]);
else
K(j,k+1) = nan;
end
E(j,k+1) = euler(k,i);
F(j) = i/2;
end
end
K=double(K);
E=double(E);
F=double(F);
F1=F.'
F1 = 11×1
0 5.000000000000000e-02 1.000000000000000e-01 1.500000000000000e-01 2.000000000000000e-01 2.500000000000000e-01 3.000000000000000e-01 3.500000000000000e-01 4.000000000000000e-01 4.500000000000000e-01
Ek=E-K;
Invers_Ek=inv(Ek);
C=Ek\F1 ;
%solusi aproximasinya
Ua=@(x)(C(1)*euler(0,x)+C(2)*euler(1,x)+C(3)*euler(2,x)+C(4)*euler(3,x)+C(5)*euler(4,x)+C(6)*euler(5,x)+C(7)*euler(6,x)+C(8)*euler(7,x)+C(9)*euler(8,x)+C(10)*euler(9,x)+C(11)*euler(10,x)) ;
Ue=@(x)((1/2)*sin(x)) ;
uaa = zeros(num_i, 1) ;
uee = zeros(num_i, 1) ;
xx = zeros(num_i, 1) ;
for j = 1:num_i
i = ivals(j);
uaa(j) = Ua(i);
uee(j) = Ue(i);
xx(j) = i;
end
y=(abs(uaa-uee));
[xx uee uaa y];
uee
uee = 11×1
0 4.991670832341408e-02 9.933466539753061e-02 1.477601033306698e-01 1.947091711543253e-01 2.397127693021015e-01 2.823212366975177e-01 3.221088436188455e-01 3.586780454497614e-01 3.916634548137417e-01
uaa
uaa = 11×1
1.091393642127514e-10 5.059912789147347e-02 1.010333878220990e-01 1.504084194311872e-01 1.982903890311718e-01 2.441975078545511e-01 3.872517270501703e-01 5.068279322003946e-01 6.579652895452455e-01 8.631624771514907e-01
y
y = 11×1
1.091393642127514e-10 6.824195680593947e-04 1.698722424568422e-03 2.648316100517412e-03 3.581217876846537e-03 4.484738552449574e-03 1.049304903526526e-01 1.847190885815491e-01 2.992872440954841e-01 4.714990223377490e-01
Warning: the line
K(j,k+1) = nan;
can potentially be invoked. You do not define the output for i < 0, or for 0.5 < i < 0.6 or for i > 1 .
With 1/10 exactly not being exactly reprentable in binary floating point formats, you need to be prepared for the possibility that when you add up 0.1's that you might end up with a value that is just slightly greater than 0.5 exactly (a number which can be represented exactly in binary floating point) or a number that is just slightly less than the closest representable number to 0.6 . The way you defined your boundaries has a gap between 0.5 (exclusive) and 0.6 (exclusive). You would have been better off defining the interval in terms of i <= 0.5 and 0.5 < i to avoid the gap and to deal with values < 0 or > 1.
Thank you for the help

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

질문:

2021년 9월 1일

댓글:

2021년 9월 5일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by