Matlab例題

1.要怎麼找LU-Factorization?

2.找inverse

3.f(x)=sinx,0≦x≦兀

g(x)=-0.4177x^2+1.3122x-0.0505

Plot f(x) and g(x) on the same coordinate. (間隔取0.1)

4.total=0;

for num=2:100

if isprime(num)

total=total+num;

end

end

fprintf('sum=%d\n',total)

The code above is an example of for-loop to calculate the sum of the primes a

mong 2 to 100.

Please create an M-file and use for-loop to do the following.

A=[17 6 8

23 5 2

18 1 13]

B=[17 0 0

23 5 2

0 0 13]

For each entry of matrix A, if it is not a prime, replace it with zero.

Matrix B is the result of the operation.

Hint:

“~” NOT operator

a( i , j); % the entry of a23

disp (a);% display the content of array a

1 Answer

Rating
  • 1 decade ago
    Favorite Answer

    1.要怎麼找LU-Factorization?

    [L,U] = lu(X)

    X:要求LU-Factorization的矩陣

    L:Lower triangular matrix

    U:Upper triangular matrix

    lu進階的使用方式可在Matlab的command window輸入help lu

    2.找inverse

    R = inv(A)

    R為A的inverse matrix

    3.

    f(x)=sinx,0<=x<=pi

    g(x)=-0.4177x^2+1.3122x-0.0505

    Plot f(x) and g(x) on the same coordinate. (間隔取0.1)

    %----for your reference code------------------%

    clc; clear all; close all;

    x=0:0.1:pi; % the interval of x sets to 0.1

    f=sin(x);

    g=-0.4177*x.^2+1.3122*x-0.0505;

    plot(x, f, 'r*-', x, g, 'go-')

    grid on; axis tight;

    legend('f(x)', 'g(x)')

    %-------------------------------------------------------%

    4.Please create an M-file and use for-loop to do the following.

    A=[17 6 8

    23 5 2

    18 1 13]

    %----for your reference code------------------%

    A=[17 6 8; 23 5 2; 18 1 13];

    [r, c]=size(A);

    for i=1: r*c

    if (~isprime(A(i)))

    B(i)=0;

    else

    B(i)=A(i);

    end

    end

    B=reshape(B, r, c);

    disp(B)

    %-------------------------------------------------------%

    Source(s): Matlab help and me
Still have questions? Get your answers by asking now.