필터 지우기
필터 지우기

I don't know why you're saying it's an invalid expression. Help me

조회 수: 1 (최근 30일)
kubel
kubel 2023년 9월 12일
편집: John D'Errico 2023년 9월 12일
clear all; close all;
x=linspace(-2*pi,2*pi,100);
y1=sin(x^2);
y2=cos^2(x);
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
figure(1)
plot(x,y1,x,y2)
grid on
title('HW#1 test -2_pi and 2_pi')
xlabel('-2pi < x < 2pi')
ylabel('sin(x^2) and cos(x)^2')
legend('y = sin(x)','y = cos(x)')

채택된 답변

Matt J
Matt J 2023년 9월 12일
편집: Matt J 2023년 9월 12일
Perhaps you intended to have this,
y1=sin(x.^2);
y2=cos(x).^2;

추가 답변 (1개)

John D'Errico
John D'Errico 2023년 9월 12일
편집: John D'Errico 2023년 9월 12일
You NEED to learn about the dotted operators, thus .*, ./, and .^ operators.
When x is a vector, for example, this:
x = 1:5
x = 1×5
1 2 3 4 5
So we see it is a row vector. MATLAB has special uses for ^, * and / that apply to linear algebra, vectors and matrices. So if you want only to square the elements of x,
x.^2
ans = 1×5
1 4 9 16 25
The dotted operator tells MATLAB to square EACH element of the result. Similarly, we could do this
x.*x
ans = 1×5
1 4 9 16 25
Again, multiply each pair of elements in turn.
But when you did this:
x^2
Error using ^
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a scalar. To operate on each element of the matrix individually, use POWER (.^) for elementwise power.
It generates an error.
I would strongly suggest the MATLAB Onramp tutorials.

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by