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

2 views (last 30 days)
% 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 Comment
Walter Roberson
Walter Roberson on 2 Sep 2021
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.'});

Sign in to comment.

Answers (1)

Relly Syam
Relly Syam on 2 Sep 2021
Edited: Walter Roberson on 2 Sep 2021
%
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 Comments
Walter Roberson
Walter Roberson on 2 Sep 2021
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!